Convert JSON to XML

When you need input data from JSON files, you first need to convert JSON files to XML files to make the data compatible with AIMMS.

Here we provide a conversion tool in the form of a custom AIMMS library. It contains a conversion AIMMS library between XML and JSON files:

  • ConvertFromJsonToXML
  • ConvertFromXMLToJson

Procedure

  1. Download JSONXML.zip.

  2. Extract the two folders and put them at the root of your project.

  3. Then add this library to your project:

    1. Open the Library Manager.
    ../../_images/step1.png
    1. Click Add existing library.
    ../../_images/step2.png
    1. Select the JSONXML folder and click Select a folder
    ../../_images/step3.png
    1. Click OK to exit the library manager.
    ../../_images/step4.png
  1. Then by calling the method jxml::ConvertFromJsonToXML you should be able to obtain your data into an XML file.

Execute the following code to create the XML file:

1
jxml::ConvertFromJsonToXML(SP_InputFile,"Answer.xml");

During the conversion, the general structure of the JSON file is conserved. For every JSON element, an XML element is created.

Note that if the JSON element had a name, then it will be set as a parameter of the corresponding XML element.

../../_images/conversion.png

Extracting data.

Extracting data from this XML requires a quite specific process. You can see in the image above that each element has an element name in the format json:Object-Type. When you’ll generate your XSD file, this notation will not be there anymore and you’ll only have Object-Type.

Hence, by generating the .AXM file using the XML schema mapping tool with that XSD, the AXM will neither have this notation. So, the XMLREAD() method will not be executed properly because AXM and XML will have different element names.

In order to solve this situation, you can follow this process:

  • Generate your XSD file as usual.
  • Create your mapping using the XML schema mapping tool
  • Edit your AXM file at the root of your project by transforming every element name ElementMapping name=”(OBJECT-TYPE)” into ElementMapping name=”json:(OBJECT-TYPE)”

Warning

be careful to transform only element names and not attributes names.

If you want more details about the general steps to extract data from an XML file you can check this article : Extract Data from an XML File.