Solved

example for reading JSON files

  • 4 March 2021
  • 4 replies
  • 104 views

I looked into the example of the dex:: library and I tried the DataExchange example. However I still have trouble to get started in my own program. I want to read a JSON file like given in the attachment and use the data in some variables in my AIMMS program. 

Could someone help me with a simple example program?

icon

Best answer by MarcelRoelofs 5 March 2021, 15:49

View original

4 replies

Hi,

Thanks a lot for this complete answer. It works and it gives me examples for some structures to build that were not explicitly mentioned in the examples. 

Looking back at the example, I think my struggle was on the "iterative-reset” and "iterative-binds-to”

Userlevel 5
Badge +2

@Jan Willem ter Weeme 

 

Here’s an example that reads this input.json with the provided mapping.xml file. See MainExecution in the project. 

 

May I ask what specifically wasn’t clear to you? Was it the identifier b, or how to actually read / write data after creating the mappings ?

Hi,

Thanks for the quick response. I fact I had different issues:

  • Basic reading did not work. I learned from the example that it had to do with my 'test’ - I did the test in a new library, leading to some problems with the reference names.
  • I am not familiar with XML nor JSON. I do not have a clear overview on how to construct the mapping. I added your simple example in my question, but the next question is how I make a mapping for a more realistic input, as given in the attachment. Some are 2-D arrays, and the indexes are never mentioned.
Userlevel 3
Badge +5

I think you’re looking for something like this:

 

<AimmsJSONMapping>
<ObjectMapping>
<ObjectMapping name="DeviceData">
<ValueMapping name="minimumMired" maps-to="minimumMired"/>
<ValueMapping name="maximumMired" maps-to="maximumMired"/>
<ValueMapping name="numberOfPWMChannels" maps-to="numberOfPWMChannels"/>
</ObjectMapping>
<ArrayMapping name="FluxConnectivityTable" iterative-reset="1">
<ArrayMapping iterative-binds-to="fc1" iterative-reset="1">
<ValueMapping iterative-binds-to="fc2" maps-to="FluxConnectivity(fc1,fc2)" force-dense="FluxConnectivityDomain(fc1,fc2)"/>
</ArrayMapping>
</ArrayMapping>
<ArrayMapping name="LedParams" iterative-reset="1">
<ObjectMapping iterative-binds-to="lp1">
<ValueMapping name="Vf" maps-to="LP_Vf(lp1)" force-dense="LP_VfDomain(lp1)"/>
<ArrayMapping name="R" iterative-reset="1">
<ValueMapping iterative-binds-to="lp2" maps-to="LP_R(lp1,lp2)" force-dense="LP_RDomain(lp1,lp2)"/>
</ArrayMapping>
</ObjectMapping>
</ArrayMapping>
</ObjectMapping>
</AimmsJSONMapping>

This works together with the following declarations in your model:

DeclarationSection DeviceData {
Parameter minimumMired;
Parameter maximumMired;
Parameter numberOfPWMChannels;
Set FluxConnectivitySet1 {
Index: fc1;
}
Set FluxConnectivitySet2 {
Index: fc2;
}
Parameter FluxConnectivity {
IndexDomain: (fc1,fc2);
}
Parameter FluxConnectivityDomain {
IndexDomain: (fc1,fc2);
Definition: 1;
}
Set LedParamSet1 {
Index: lp1;
}
Set LedParamSet2 {
Index: lp2;
}
Parameter LP_Vf {
IndexDomain: lp1;
}
Parameter LP_VfDomain {
IndexDomain: lp1;
Definition: 1;
}
Parameter LP_R {
IndexDomain: (lp1,lp2);
}
Parameter LP_RDomain {
IndexDomain: (lp1,lp2);
Definition: 1;
}
}

I tested this to work properly with your given input json file, and writing back the data will generate the same json file.

The entries for the “force-dense” attribute are there to make sure that output is also generated when the corresponding identifiers hold default values. They are of no consequence when reading the data.

 

 

Reply


Didn't find what you were looking for? Try searching on our documentation pages:

AIMMS Developer & PRO | AIMMS How-To | AIMMS SC Navigator