xmltojson
Service icon

XmlToJson

Stable version 1.0.2 (Compatible with OutSystems 11)
Other versions available for 10
Uploaded
 on 30 November 2022
 by 
4.9
 (11 ratings)
xmltojson

XmlToJson

Documentation
1.1.0

XmlToJson

Introduction

XmlToJson is a simple extension that allows the programmer to convert an XML document to the equivalent JSON. This makes it easier to deal with the data in OutSystems, as it's much easier to deal with JSON than XML.

Since version 1.0.2 it is possible to specify the (local) node names that are to be treated as arrays, even if the XML contains only a single element. This will help with situations where you may sometimes have a single element and sometimes multiple. Without specifying the node to be treated as an array, the JSON will sometimes contain an object, sometimes an array of objects, which means you cannot convert the JSON to a structure easily.

Version 1.1.0 adds some extra input parameters to control the output.

The extension uses NewtonSoft.Json's JsonConvert.SerializeXmlNode function for the heavy lifting, meaning that any limitations this asset has are due to that function.

Converting XML to JSON

The extension has a single action, XmlToJson (in version prior to 1.02 this was called toJSON), that has the following parameters:

  • XML - input parameter of type Text that contains the XML document. It must be valid XML, or it cannot be parsed. Note that any namespaces are parsed as well and included in the JSON. Any attributes are included as well, with an "@" in front of the name;
  • ArrayNodes - optional input parameter of type list of "Node" structures. The "Node" structure contains a single attribute "Name". If specified, each node in the XML document that has a local name that is equal to one of the node names in the list will become an array in the output JSON;
  • RemoveNamespacePrefixes - With this boolean parameter one can specify that namespace prefixes are removed before the XML is converted to JSON. Node names containing a namespace prefix can't be deserialized in a module, so removing them is typically desirable. Therefore this parameter has a default of True.
  • IsFormatted - If True, the JSON output will be in human readable form, with seperate lines and indented where needed, instead of a single line. Ideal for debugging.
  • OmitRoot - If True, the root element of the XML is omitted in the output JSON. This removes the need to specify the root element in your output structure when deserializing.
  • JSON - output parameter of type Text that contains the converted JSON.


The resulting JSON can be used to create structures from (design time), then serialized (run time). This is much easier than trying to serialize the XML.


1.0.2

XmlToJson

Introduction

XmlToJson is a simple extension that allows the programmer to convert an XML document to the equivalent JSON. This makes it easier to deal with the data in OutSystems, as it's much easier to deal with JSON than XML.

Since version 1.02 it is possible to specify the (local) node names that are to be treated as arrays, even if the XML contains only a single element. This will help with situations where you may sometimes have a single element and sometimes multiple. Without specifying the node to be treated as an array, the JSON will sometimes contain an object, sometimes an array of objects, which means you cannot convert the JSON to a structure easily.

The extension uses NewtonSoft.Json's JsonConvert.SerializeXmlNode function for the heavy lifting, meaning that any limitations this asset has are due to that function.

Converting XML to JSON

The extension has a single action, XmlToJson (in version prior to 1.02 this was called toJSON), that has the following parameters:

  • XML - input parameter of type Text that contains the XML document. It must be valid XML, or it cannot be parsed. Note that any namespaces are parsed as well and included in the JSON. Any attributes are included as well, with an "@" in front of the name;
  • ArrayNodes - optional input parameter of type list of "Node" structures. The "Node" structure contains a single attribute "Name". If specified, each node in the XML document that has a local name that is equal to one of the node names in the list will become an array in the output JSON;
  • JSON - output parameter of type Text that contains the converted JSON.


The resulting JSON can be used to create structures from (design time), then serialized (run time). This is much easier than trying to serialize the XML.