XML to JSON Simple component
The XML to JSON Simple component allows you to convert XML files into JSON files.
Configuration
The XML to JSON Simple component has the following basic configuration options:
Keep strings?
By default, the component tries to infer the type of each value and converts it into a string
, number
, or boolean
. When this option is set to Yes
, all values are transformed into strings
, encapsulated in double quotes ("value"
).
Options
Yes
No
(default)
When used in combination with Has types? set to Yes
, all values will be treated as strings
, except for those explicitly modified using type attributes.
- XML Input
- JSON Output - No
- JSON Output - Yes
<?xml version="1.0" encoding="UTF-8"?>
<Formula1>
<Drivers>
<Driver>
<Name>Max Verstappen</Name>
<Age>19</Age>
</Driver>
<Driver>
<Name>Nico Hulkenberg</Name>
<Age>30</Age>
</Driver>
</Drivers>
<Teams>
<Team>
<Name>Redbull Racing</Name>
<Principal>Christian Horner</Principal>
</Team>
<Team>
<Name>Renault</Name>
<Principal>Carlos Ghosn, Jérôme Stoll, Cyril Abiteboul</Principal>
</Team>
</Teams>
</Formula1>
{
"Formula1": {
"Drivers": {
"Driver": [
{
"Age": 19,
"Name": "Max Verstappen"
},
{
"Age": 30,
"Name": "Nico Hulkenberg"
}
]
},
"Teams": {
"Team": [
{
"Principal": "Christian Horner",
"Name": "Redbull Racing"
},
{
"Principal": "Carlos Ghosn, Jérôme Stoll, Cyril Abiteboul",
"Name": "Renault"
}
]
}
}
}
{
"Formula1": {
"Drivers": {
"Driver": [
{
"Age": "19",
"Name": "Max Verstappen"
},
{
"Age": "30",
"Name": "Nico Hulkenberg"
}
]
},
"Teams": {
"Team": [
{
"Principal": "Christian Horner",
"Name": "Redbull Racing"
},
{
"Principal": "Carlos Ghosn, Jérôme Stoll, Cyril Abiteboul",
"Name": "Renault"
}
]
}
}
}
Remove namespaces?
Specify whether to remove all namespace attributes and prefixes from the resulting JSON.
Options
Yes
No
(default)
- XML Input
- JSON Output - No
- JSON Output - Yes
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<h:table xmlns:h="http://www.dovetail.world/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="https://www.dovetail.world/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
{
"root": {
"h:table": {
"h:tr": {
"h:td": [
"Apples",
"Bananas"
]
},
"xmlns:h": "http://www.dovetail.world/TR/html4/"
},
"f:table": {
"f:width": 80,
"f:length": 120,
"xmlns:f": "https://www.dovetail.world/furniture",
"f:name": "African Coffee Table"
}
}
}
{
"root": {
"table": [
{
"tr": {
"td": ["Apples", "Bananas"]
}
},
{
"name": "African Coffee Table",
"width": 80,
"length": 120
}
]
}
}
Remove root object?
Specify whether to remove the root object from the JSON output.
Options
Yes
No
(default)
- XML Input
- JSON Output - No
- JSON Output - Yes
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<person>
<name>John Johnson</name>
<age>19</age>
</person>
<person>
<name>John Foo</name>
<age>30</age>
</person>
<person>
<name>John Doe</name>
<age>30</age>
</person>
</root>
{
"root": {
"person": [
{
"name": "John Johnson",
"age": 19
},
{
"name": "John Foo",
"age": 30
},
{
"name": "John Doe",
"age": 30
}
]
}
}
{
"person": [
{
"name": "John Johnson",
"age": 19
},
{
"name": "John Foo",
"age": 30
},
{
"name": "John Doe",
"age": 30
}
]
}
Has Types?
When set to Yes
, this option allows you to define a type attribute in the XML elements, determining the type of their values in the JSON output.
Options
Yes
No
(default)
The available types are string
, number
, boolean
and array
. If an unrecognized type is specified, the value defaults to string
.
- XML Input
- JSON Output - No
- JSON Output - Yes
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<person>
<name>John Johnson</name>
<age type="string">19</age>
</person>
<person>
<name>John Foo</name>
<age type="string">30</age>
</person>
<person>
<name>John Doe</name>
<age type="string">30</age>
</person>
</root>
{
"root": {
"person": [
{
"name": "John Johnson",
"age": {
"type": "string",
"jsonContent": 19
}
},
{
"name": "John Foo",
"age": {
"type": "string",
"jsonContent": 30
}
},
{
"name": "John Doe",
"age": {
"type": "string",
"jsonContent": 30
}
}
]
}
}
{
"root": {
"person": [
{
"name": "John Johnson",
"age": "19"
},
{
"name": "John Foo",
"age": "30"
},
{
"name": "John Doe",
"age": "30"
}
]
}
}
- XML Input
- JSON Output
<?xml version="1.0" encoding="UTF-8"?>
<Formula1>
<Drivers>
<Driver type="array">
<Name>Max Verstappen</Name>
<Age>19</Age>
</Driver>
<Driver type="array">
<Name>Nico Hulkenberg</Name>
<Age>30</Age>
</Driver>
</Drivers>
<Teams type="array">
<Team type="array">
<Name>Redbull Racing</Name>
<Principal>Christian Horner</Principal>
</Team>
<Team type="array">
<Name type="string">Renault</Name>
<Principal>Carlos Ghosn, Jérôme Stoll, Cyril Abiteboul</Principal>
</Team>
</Teams>
</Formula1>
{
"Formula1": {
"Drivers": {
"Driver": [
[
19,
"Max Verstappen"
],
[
30,
"Nico Hulkenberg"
]
]
},
"Teams": [
[
[
"Christian Horner",
"Redbull Racing"
],
[
"Carlos Ghosn, Jérôme Stoll, Cyril Abiteboul",
"Renault"
]
]
]
}
}
When the type and value don't match it needs to
Specify how mismatches between an XML element's type and its value should be handled.
Options
Keep the original value
(default)Set the value to null
Go to the error route
Only available when Has Types? is enabled.
- XML Input
- JSON Output - Original value
- JSON Output - Set null
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<person>
<name type="number">John Johnson</name>
<age>19</age>
</person>
<person>
<name type="number">John Foo</name>
<age>30</age>
</person>
<person>
<name type="number">John Doe</name>
<age>30</age>
</person>
</root>
{
"root": {
"person": [
{
"name": "John Johnson",
"age": 19
},
{
"name": "John Foo",
"age": 30
},
{
"name": "John Doe",
"age": 30
}
]
}
}
{
"root": {
"person": [
{
"name": null,
"age": 19
},
{
"name": null,
"age": 30
},
{
"name": null,
"age": 30
}
]
}
}
When Go to the error route
is selected, messages with mismatches are sent to the Error Route and the following error is logged in the Flow Logs:
org.assimbly.util.exception.JsonTypeException:
There was a mismatch between a specified type and the value.
Type is 'number' and the value is 'John Johnson'.
Using XML to JSON Simple
XML attributes
XML attributes are converted into an object as key-value pairs. Each attribute's name becomes a key, prefixed with an @
(at) symbol to indicate it was originally an XML attribute. The value of the key is the attribute's original value. Additionally, the object includes the original XML node value in a key named jsonContent
.
- XML Input
- JSON Output
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<person>
<name realName="true">John Johnson</name>
<age>19</age>
</person>
<person>
<name realName="false">John Foo</name>
<age>30</age>
</person>
<person>
<name realName="false">John Doe</name>
<age>30</age>
</person>
</root>
{
"root": {
"person": [
{
"name": {
"jsonContent": "John Johnson",
"@realName": "true"
},
"age": "19"
},
{
"name": {
"jsonContent": "John Foo",
"@realName": "false"
},
"age": "30"
},
{
"name": {
"jsonContent": "John Doe",
"@realName": "false"
},
"age": "30"
}
]
}
}