XML.ToJSON
Converts XML to JSON.
Component |
Version |
macOS |
Windows |
Linux |
Server |
iOS SDK |
XML
|
8.2 |
✅ Yes |
✅ Yes |
✅ Yes |
✅ Yes |
✅ Yes |
MBS( "XML.ToJSON"; XML { ; Flags } )
More
MBS(
"XML.ToJSON"; /* Converts XML to JSON. */
$XML; /* The XML to process.e.g. "<test>Hello</test>" */
$Flags) /* Optional; Various Flags.
Add 1 to ignore errors in xml and continue parsing. This may lead to not everything in the xml being read.
Add 2 to put attributs inline.
Add 4 to make entries with true/false being boolean.
Add 8 to make entries with numbers as numbers.
Add 16 to include namespaces.
Add 32 to use empty text for empty xml nodes.e.g. 0 */
Less
Parameters
Parameter |
Description |
Example |
Flags |
XML |
The XML to process. |
"<test>Hello</test>" |
|
Flags |
Various Flags.
Add 1 to ignore errors in xml and continue parsing. This may lead to not everything in the xml being read.
Add 2 to put attributs inline.
Add 4 to make entries with true/false being boolean.
Add 8 to make entries with numbers as numbers.
Add 16 to include namespaces.
Add 32 to use empty text for empty xml nodes. |
0 |
Optional |
Result
Returns JSON or error.
Description
Converts XML to JSON.
For nodes in the XML we create objects and key/values.
Attributes are put in @attributes nodes.
Namespaces are stripped.
Added option flag 2 in version 10.5 to not use @attributes object, but put attributes directly in objects with "@" prefix. The JSON.ToXML function unpacks that.
Added option flags 4 and 8 for version 11.2 to handle boolean and numbers.
Added option flag 16 in version 12.1 to include namespaces.
Examples
Try it:
Set Variable [ $XML ; Value: MBS( "JSON.ToXML"; "{\"Hello\": \"World\"}"; "test") ]
Show Custom Dialog [ $xml ]
Set Variable [ $JSON ; Value: MBS( "XML.ToJSON"; $XML) ]
Show Custom Dialog [ $json ]
Try with boolean and number detection:
MBS( "XML.ToJSON";
"<test>
<value>123</value>
<value>45.67</value>
<value>true</value>
<value>false</value>
<value>Hello</value>
</test>"; 4 + 8 )
Example result:
{
"test": {
"value": [123, 45.67, true, false, "Hello"]
}
}
See also
Release notes
- Version 13.2
- Added flag 32 to XML.ToJSON function to keep empty nodes as text nodes instead of empty objects.
- Fixed a problem with empty attributes causing an error in JSON for XML.ToJSON function.
- Version 12.5
- Changed XML.ToJSON to better convert single values, which are boolean or number and not just text.
- Version 12.4
- Version 12.1
- Added new flag for XML.ToJSON to include namespaces.
- Version 11.2
- Added new flags for XML.ToJSON to detect numbers and booleans better.
- Version 10.5
- Changed XML.ToJSON and added option for compacter attributes processing.
- Version 10.4
- Version 10.0
- Version 9.5
Example Databases
Blog Entries
This function checks for a license.
Created 27th April 2018, last changed 13th April 2023
XML.ToHTML
-
XML.Validate