Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
  • 1.  How to turn json files into dat files

    Posted Fri March 16, 2018 11:44 AM

    Hi

    common question : I have a json file and I need to turn that into a .dat so that I can run OPL

    Let me show you the way.

    You have

    Nurses.json

    {"Nurses":[
    {"name":"Anne",
    "seniority":11,
    "qualification":1,
    "payRate":25
    },
    {"name":"Bethanie",
    "seniority":4,
    "qualification":5,
    "payRate":28
    },
    {"name":"Betsy",
    "seniority":2,
    "qualification":2,
    "payRate":17
    }
    ]
    }

     

    and

    Spokes.json

    {"Spokes":[
    {"name":"A",
    "minDepTime":360,
    "maxArrTime":1080
    },
    {"name":"B",
    "minDepTime":400,
    "maxArrTime":1150
    }
    ]
    }

     

    then if you run readJSON.py

    import json

    def JSonToTupleSet(jsonfilename,datname):
        with open(jsonfilename) as data_file:    
             data = json.load(data_file)
     
        # write that into a .dat
     
        res = open(datname, "w")

        quote='"'


        for i in data:
            ii=data[i]
        
            res.write(i);
            res.write("={")
            res.write("\n")

            for j in ii:
          
              res.write("<")
              for j2 in j:
                 jj=j[j2]
             
                 if (jj==jj):
                     if (type(jj)==str):
                       res.write("\"")
                     res.write(str(jj))
                     if (type(jj)==str):
                        res.write("\"")
                     res.write(",")
              res.write(">,")    
              res.write("\n")
        
     
            res.write("};")
       
        res.close()


    JSonToTupleSet("Nurses.json","nurses.dat")
    JSonToTupleSet("Spokes.json","spokes.dat")

     

    then you ll get

    Nurses.dat

    Nurses={
    <"Anne",11,1,25,>,
    <"Bethanie",4,5,28,>,
    <"Betsy",2,2,17,>,
    };

    and

    Spokes.dat

    Spokes={
    <"A",360,1080,>,
    <"B",400,1150,>,
    };

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: How to turn json files into dat files

    Posted Wed October 10, 2018 09:37 PM

    Originally posted by: AndyHam


    Hi Alex,
    I was looking for the way to read JSON file from OPL.
    This posting helps me to convert JSON file to DAT file.
    However, is there any way to use JSON file directly in OPL without this kind of conversion?
    Thanks,
    Andy


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: How to turn json files into dat files

    Posted Thu October 11, 2018 02:41 AM

    Hi,

    with docplexcloud yes you may use json as can be seen at https://developer.ibm.com/docloud/documentation/docloud/opl-model-input-and-output/

    With OPL on your machine not yet

    But do not hesitate to log a wish : https://ibmanalytics.ideas.aha.io/?project=CPLEX

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: How to turn json files into dat files

    Posted Thu October 11, 2018 08:50 AM

    Originally posted by: AndyHam


    Thanks for the info!
    I have just logged a wish ^^
    https://ibmanalytics.ideas.aha.io/ideas?project=DOPTC


    #DecisionOptimization
    #OPLusingCPLEXOptimizer