XML.ToJSON
----------

Converts XML to JSON.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [XML](component_XML.md) | [8.2](newinversion82.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "XML.ToJSON"; XML { ; Flags } ) 

**MBS**( **"XML.ToJSON";** /\* Converts XML to JSON. \*/  
**$XML**; /\* The XML to process.e.g. "&lt;test&gt;Hello&lt;/test&gt;" \*/   
**$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 \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| XML | The XML to process. | "&lt;test&gt;Hello&lt;/test&gt;" |  |
| 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](JSONToXML.md) 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](JSONToXML.md)"; "{\\"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";   
"&lt;test&gt;  
 &lt;value&gt;123&lt;/value&gt;  
 &lt;value&gt;45.67 &lt;/value&gt;  
 &lt;value&gt;true&lt;/value&gt;  
 &lt;value&gt;false&lt;/value&gt;  
 &lt;value&gt;Hello&lt;/value&gt;  
&lt;/test&gt;"; 4 + 8 )  
<small>  
Example result:   
{ "test" : { "value" : \[123 , 45.67 , true, false, "Hello" \] } } </small>### See also

- [JSON.ToXML](JSONToXML.md)
- [XML.ToHTML](XMLToHTML.md)

### Release notes

- **Version 13.2**
    - Added flag 32 to [XML.ToJSON](https://www.mbsplugins.eu/XMLToJSON.shtml) 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](https://www.mbsplugins.eu/XMLToJSON.shtml) function.
- **Version 12.5**
    - Changed [XML.ToJSON](https://www.mbsplugins.eu/XMLToJSON.shtml) to better convert single values, which are boolean or number and not just text.
- **Version 12.4**
    - Changed [JSON.ToXML](https://www.mbsplugins.eu/JSONToXML.shtml) to unpack the text nodes made by [XML.ToJSON](https://www.mbsplugins.eu/XMLToJSON.shtml).
    - Improved [XML.ToJSON](https://www.mbsplugins.eu/XMLToJSON.shtml) to handle data type conversion for attributes, too.
- **Version 12.1**
    - Added new flag for [XML.ToJSON](https://www.mbsplugins.eu/XMLToJSON.shtml) to include namespaces.
- **Version 11.2**
    - Added new flags for [XML.ToJSON](https://www.mbsplugins.eu/XMLToJSON.shtml) to detect numbers and booleans better.
- **Version 10.5**
    - Changed [XML.ToJSON](https://www.mbsplugins.eu/XMLToJSON.shtml) and added option for compacter attributes processing.
- **Version 10.4**
    - Changed [JSON.ToXML](https://www.mbsplugins.eu/JSONToXML.shtml) to recreate attributes stored as @attributes entry by [XML.ToJSON](https://www.mbsplugins.eu/XMLToJSON.shtml) function.
- **Version 10.0**
    - Improved CData handling for [XML.ToJSON](https://www.mbsplugins.eu/XMLToJSON.shtml) function.
- **Version 9.5**
    - Fixed bug in [XML.ToJSON](http://www.mbsplugins.eu/XMLToJSON.shtml) function.

### Example Databases

- [XML/XML and JSON](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/XML/XML%20and%20JSON.shtml#2ScriptAnchor_)
- [XML/XML to JSON](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/XML/XML%20to%20JSON.shtml#1292FieldAnchor_)

### Blog Entries

- [MBS FileMaker Plugin, version 13.2pr4](https://www.mbsplugins.de/archive/2023-04-25/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 13.2pr3](https://www.mbsplugins.de/archive/2023-04-20/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 12.5pr1](https://www.mbsplugins.de/archive/2022-09-29/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [Convert XML to JSON](https://www.mbsplugins.de/archive/2022-09-08/Convert_XML_to_JSON/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 12.4pr3](https://www.mbsplugins.de/archive/2022-08-24/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 12.4pr1](https://www.mbsplugins.de/archive/2022-08-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [What is new in the MBS FileMaker Plugin Version 10.3](https://www.mbsplugins.de/archive/2020-07-31/What_is_new_in_the_MBS_FileMak/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin 10.0 - More than 6000 Functions In One Plugin](https://www.mbsplugins.de/archive/2020-01-14/MBS_FileMaker_Plugin_100_-_Mor/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin 8.2 - More than 5200 Functions In One Plugin](https://www.mbsplugins.de/archive/2018-05-22/MBS_FileMaker_Plugin_82_-_More/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin Schulung in 2018](https://www.mbsplugins.de/archive/2018-05-02/MBS_FileMaker_Plugin_Schulung_/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 3/2025, Seite 22](https://filemaker-magazin.de/neuigkeit/4290-Appetithappen-FMM_202503)
- [Ausgabe 6/2022, Seite 24 bis 25](https://filemaker-magazin.de/neuigkeit/4206-Appetithappen-FMM_202206)

This function checks for a license.

Created 27th April 2018 , last changed 13th April 2023

  
[XML.ToHTML](XMLToHTML.md) - [XML.Validate](XMLValidate.md)

[HTML Version](XMLToJSON.shtml)