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
WindowsLinuxJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac Catalyst
Others
Stored procedures
JSONXMLConverter (Variable type)
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 and JSONToXML). You can define and change the characteristics of this conversion using different WLanguage properties.
Note: 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 ] }
Properties

Properties specific to JSONXMLConverter variables

The following properties can be used to handle a converter:
Property nameType usedEffect
ArrayConversionWithRootBoolean
  • True (default) to convert JSON arrays to XML using the name of the JSON element as root name,
  • False otherwise.
ElementName Character stringName of elements for arrays with a 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.
PrefixAttributeCharacter stringPrefix of XML attributes in JSON. By default, this property is set to "@".
PrefixCDATACharacter stringKey for escaping an XML section. By default, this property is set to "CDATA".
PrefixNamespaceDeclaredCharacter stringPrefix for declared namespaces. By default, this property is set to "%".
PrefixNamespaceNodeCharacter stringPrefix for the node namespace. By default, this property is set to "$".
PrefixTextCharacter stringPrefix of the text of XML text nodes in JSON. By default, this property is set to "#text".
XMLRootNameCharacter 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: JSON null value.
  • XML attribute: "@" + name of the XML attribute converted to a JSON member of the object representing 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 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: JSON syntax has no 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: JSON syntax does not have a namespace for keys. A simple solution is to interpret them as standard names and attributes.
  • Empty tag: JSON syntax does not allow for keys without a value. There are two possible representations: null 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 any attributes can lead to the creation of 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: 06/11/2025

Send a report | Local help