XML to JSON Simple component
The XML to JSON Simple component provides a direct conversion from XML format to the JSON format.
Configuration
It has the following configuration options:
- Keep strings?
- Remove namespaces?
- Remove root object?
- Has types?
-
When the type and value do not match...
Keep in mind that each enabled option could take some extra time to complete the transformation.
Keep strings?
Options
yes
no
(default)
Description
By default the component tries to infer the type of the value and generates it is
a string, number or boolean. When you set this option to yes
it just transforms
all the values to a string by setting double quotes(""
) around it.
Remark
When used in combination with Has types? set to yes
,
types set with type
attributes still work so you get a combination of both
options. Everything will be a string except the values you change with the type
attributes.
Example
Notice the difference for the age of the drivers. When no
is selected they are
transformed as numbers. When yes
is selected they are transformed into strings.
- Source XML
- JSON - 'no' is selected
- JSON - 'yes' is selected
<?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?
Options
yes
no
(default)
Description
When you set this option to yes
it will remove all namespace attributes and
prefixes from the resulting JSON. The removed namespaces and its prefixes will
also be mentioned in the flow log.
Example
- Source XML
- JSON - 'no' is selected
- JSON - 'yes' is selected
<?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?
Options
yes
no
(default)
Description
When you set this option to yes
it will remove the root object from the resulting
processed JSON.
Example
- Source XML
- JSON - 'no' is selected
- JSON - 'yes' is selected
<?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?
Options
yes
no
(default)
Description
When you set this option to yes
you can add type attributes to your XML elements
which determine the types of the values of these elements in the resulting JSON.
The available types are string
, number
, boolean
and array
. When an unrecognized
type is used it will add a warning in the flow logs and use the type string
.
When a type is used that is not supported this will be logged in the log as a
warning and the type string
will be used.
Simple example
- Source XML
- JSON - 'no' is selected
- JSON - 'yes' is selected
<?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"
}
]
}
}
Example with array types
- Source XML
- Resulting JSON
<?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 do not match
Prerequesites
To enable this option, the Has types? option should be
set to yes
.
Options
keep the original value
(default)set the value to null
go to the error route
Description
This option determines how a mismatch between the type and value of an XML element is handled.
Example
- Source XML
- JSON - 'keep the original value' is selected
- JSON - 'set the value to null' is selected
<?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 it will log the following error in the
logs and go to the error route:
world.dovetail.common.exception.JsonTypeException: There was a mismatch between
a specified type and the value. Type is 'number' and the value is 'John Johnson'.
Remarks
XML attributes
XML attributes are put in an object as a key value pair were the key is prefixed
with a @
symbol to indicate it was an XML attribute. The value if the XML node
will also be put in the same object with the key jsonContent
.
Example
- Source XML
- Resulting JSON
<?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"
}
]
}
}