ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / XML file functions
  • Properties specific to JSONXMLConverter variables
  • JSON to XML and XML to JSON conversion standard
  • Default conversion standard
  • Special conversion cases
  • WLanguage functions that use the JSONXMLConverter type
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
JSONXMLConverter (Type of variable)
In french: JSONXMLConvertisseur
The JSONXMLConverter type is used to define all the advanced characteristics of the conversion of an XML object to JSON (or conversely) via XMLToJSON et JSONToXML). The characteristics of this conversion can be defined and changed using different WLanguage properties.
Remark: For more details on the declaration of this type of variable and the use of WLanguage properties, see Declaring a variable.
Example
let json = StringToJSON("
{"name": "TestName"
 ,"id": 2
 ,"true": true
 ,"list": [5,5,6,2,47]
}
")
 
let converter is JSONXMLConverter
converter.ArrayConversionWithRoot = False
converter.FullConversion = True
let xml = JSONToXML(json, converter)
Trace(xml..XMLSource)
 
// The result will be
// <document>
//   <name>TestName</name>
//   <id>2</id>
//   <true>true</true>
//   <list>5</list>
//   <list>5</list>
//   <list>6</list>
//   <list>2</list>
//   <list>47</list>
// </document>
 
Trace(XMLToJSON(xml, converter))
 
// the result will be:
// { "name":"TestName", "id":2, "true":true, "list":[ 5, 5, 6, 2, 47 ] }
Remarks

Properties specific to JSONXMLConverter variables

The following properties can be used to handle a converter:
Property nameType usedEffect
ArrayConversionWithRoot Boolean
  • True (default) to convert JSON arrays to XML using the name of the JSON element as root name,
  • False otherwise.
ElementName Character stringElement names for arrays with root.
EmptyTagRepresentation Boolean
  • True (default value) if the empty tag is represented by NULL,
  • False otherwise.
FullConversion Boolean
  • True (default) if the conversion must include attributes and namespaces,
  • False to perform a simple conversion (ignoring attributes and namespaces).
JSONTyping Boolean
  • True (default value) if types are deducted when converting to JSON,
  • False otherwise.
PrefixAttribute Character stringPrefix of XML attributes in JSON. By default, this property is set to "@".
PrefixCDATA Character stringKey for escaping an XML section. By default, this property is set to "CDATA".
PrefixNamespaceDeclared Character stringPrefix for declared namespaces. By default, this property is set to "%".
PrefixNamespaceNode Character stringPrefix for the node namespace. By default, this property is set to "$".
PrefixText Character stringPrefix of the text of XML text nodes in JSON. By default, this property is set to "#text".
XMLRootName Character stringXML root name. By default, this property is set to "root".
Remarks

JSON to XML and XML to JSON conversion standard

There are no specific standards for either type of conversion. However, there are two methods to define conversion operations:
  • Forcing a standard the during conversion (using the default values of the JSONXMLConverter variable).
  • Using a custom conversion standard with a variable of type JSONXMLConverter to describe the conversion rules.

Default conversion standard

XML to JSON
  • XML tag: "" + tag name followed by a JSON object.
  • Empty tag: null JSON value.
  • XML attribute: "@" + attribute name as member of the JSON object that represents the tag.
  • Text node: simple key/value pair if there are no attributes, "#text" if the node is a JSON object.
  • Escape sections: "CDATA" with the string as JSON value.
JSON to XML
  • Empty object: empty tag if the object is named.
  • Empty list: empty tag if the array is named.
  • Value: text node.

Special conversion cases

JSON - XML conversion is not a common and lossless conversion, since the two languages have different syntax and data. Any conversion will require multiple adaptations to keep the data while limiting losses from the original document.
The following is a list of the various conflict points and ambiguities that can occur during a conversion, as well as suggested solutions that allow for a reverse conversion (if possible) without compromising object data.
XML to JSON
  • XML attribute: The JSON syntax does not have attributes for key-value pairs. To keep the notion of attributes, one solution is to prefix them in the JSON content and place them as object members.
  • Namespace: The JSON syntax does not have a namespace for keys. A simple solution is to interpret them as standard names and attributes.
  • Empty tag: The JSON syntax does not allow for keys without a value. There are two possibilities: represent them with the null value or an empty object (in the case of an empty tag with attributes, the only possible representation is the pseudo-empty object, containing only the attributes of the tag).
  • Text node: Text nodes can be represented as a key-value pairs, but if there are attributes, they can be transformed into an object with the value of the key that represents the node. In order to keep a reference to the textual value of the node, one solution consists in creating an internal couple containing the text of the node.
  • List of nodes: XML lists are easily transposed to JSON, but since it is not possible to duplicate JSON keys, the order of child nodes may be lost in some cases.
  • Escape sections: In JSON, there are no "escape sections", but it is possible to store them as special fields in the target JSON content, similar to text nodes.
  • Comments: In JSON, there are no comments. The solution is to simply not copy them to the target JSON content, since they are optional.
JSON to XML
  • Value types: XML documents do not have data types. These types will be lost; otherwise, the original types must be stored in the generated XML document.
  • Empty list: Technically speaking, XML does not have empty lists. Considering them as empty tags implies a risk of conflict with empty and/or null objects.
  • Empty object / null: An empty XML tag can be used to represent these JSON concepts, but for reverse conversion purposes, only one of the two representations must be used.

WLanguage functions that use the JSONXMLConverter type

JSONToXMLConverts a JSON string to XML.
XMLToJSONConverts an XML string to JSON.
Minimum version required
  • Version 27
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 01/26/2023

Send a report | Local help