Skip to content
  • There are no suggestions because the search field is empty.

How-To: Convert XML to JSON

What Does This Article Cover?

  • Convert XML data to JSON format
  • Example
  • Considerations
  • Other related material

Convert XML data to JSON format

The following covers an example of converting XML formatted data into a JSON structure using a third party npm library "fast-xml-parser". Importing an npm library is covered here.

  • Enable expression-imports on the Settings page. alt text
  • This will create a /appData/expression-imports folder alt text
  • Run the following command to install the npm package from the /expression-imports folder, or copy and paste the library files into the directory:
npm install fast-xml-parser

alt text

Example

Read in the XML file data and toggle on metadata:

Create a Custom Condition and add the XML input to the Source:

alt text

The expression below can be pasted into a Custom Condition. The {{this.currentValue}} evaluates to the data from the Source, which in this case is the XML file data.

//Load the XML string from the File input
var XML_String = {{this.currentValue}}.value.utf8String

//Import the library for converting xml to json
var {XMLParser} = require("fast-xml-parser");

var options = {
    allowBooleanAttributes: true, //include attributes without values
    //alwaysCreateTextNode: true, //assign element values to a text property
    attributesGroupName : "@_", //group all attributes - suggested in case attribute and element have same name
    attributeNamePrefix : '', //add prefix to attributes - suggested is attribute grouping is not used
    //attributeValueProcessor: (name, val, jPath) => {:} //checkdoc
    //cdataPropName: "__cdata", //group CDATA values, otherwise values will be merged with text value
    //commentPropName: "#comment", //include comments
    //htmlEntities: false,
    ignoreAttributes : false,
    ignoreDeclaration: true,
    ignorePiTags: true,
    //isArray: (name, jpath, isLeafNode, isAttribute) => {} //checkdoc
    //numberParseOptions //checkdoc
    parseAttributeValue: true, //otherwise all attribute values will be string
    parseTagValue: true, //otherwise all tag values will be string
    //preserveOrder: true, //difficult to query, but maintains order
    processEntities: false, //process DTD schema
    //removeNSPrefix: false, //retain namespace values
    //stopNodes: ["root.a"] //limit depth for up to where to parse
    //tagValueProcessor: (tagName, tagValue, jPath, hasAttributes, isLeafNode) => {} //check doc
    textNodeName: "content", //specify text node name
    //transformTagName: (tagName) => tagName.toLowerCase(), //checkdoc
    //transformAttributeName: (attributeName) => attributeName.toLowerCase(), //checkdoc
    //trimValues: false, //remove whitespace around values
    //unpairedTags: ["unpairedtag1","unpairedtag2"] //checkdoc - specify in advance which tags are unpaired
    //updateTag(tagName, jPath, attrs){ } //checkdoc
};

//Initialize an Instance of the parser class with the specified option settings
var parser = new XMLParser(options);

//parse and convert the xml string to json
var jObj = parser.parse(XML_String);

//return the JSON object
jObj;





Considerations

The desired JSON structure may vary based on your needs. Give the "fast-xml-parser" library a read to see all of its options. The various available configuration options specific to this library are added for your convenience and commented out besides the ones that are typically used for querying the XML data.

Feel free to reach out to us if you have any issues or have any questions.

Other related material: