# JSON to XML correct syntax

**URL:** https://discourse.nodered.org/t/json-to-xml-correct-syntax/82630
**Category:** General
**Created:** [5 November 2023 22:17 UTC](https://discourse.nodered.org/t/json-to-xml-correct-syntax/82630 "2023-11-05T22:17:12Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jttjtt](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@jttjtt](https://discourse.nodered.org/u/jttjtt)
#### Post date: [5 November 2023 22:17 UTC](https://discourse.nodered.org/t/json-to-xml-correct-syntax/82630/1 "2023-11-05T22:17:12Z")

</div>

I am using XML node to make XML file from JSON input.  
If i make this JSON:

```auto
{
    "Label": {
        "$": {
            "Key": "Unique1"
        },
        "_": "Test"
    }
}

```

I get this XML:

```auto
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Label Key="Unique1">Test</Label>

```

But i am wondering how to write the JSON to get this XML:

```auto
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Label Key="Unique1">Test</Label>
<Label Key="Unique2">Test2</Label>
<Label Key="Unique3">Test3</Label>

```

I am always getting DuplicateKeyWarning in JSON editor in Node red and do not know how to write it correctly. Thanks for help.

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [5 November 2023 23:21 UTC](https://discourse.nodered.org/t/json-to-xml-correct-syntax/82630/2 "2023-11-05T23:21:23Z")

</div>

Well, this is a total guess... but I would try to pass in an array of label objects, something like this:

```auto
{
    "Label": [
        {
            "$": { "Key": "Unique1" },
            "_": "Test"
        },
        {
            "$": { "Key": "Unique2" },
            "_": "Test2"
        },
        {
            "$": { "Key": "Unique3" },
            "_": "Test3"
        }
    ]
}

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [5 November 2023 23:45 UTC](https://discourse.nodered.org/t/json-to-xml-correct-syntax/82630/3 "2023-11-05T23:45:41Z")

</div>

I would probably just write a function and a simple object

```auto
[{"id":"649e8db5cc0d57f4","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"Unique1\":\"Test\",\"Unique2\":\"Test2\"}","payloadType":"json","x":170,"y":1240,"wires":[["4ed0322bc6e98d33"]]},{"id":"4ed0322bc6e98d33","type":"function","z":"d1395164b4eec73e","name":"function 146","func":"let output = '<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>';\nObject.entries(msg.payload).forEach(arr => {\n output += `\\n<Label Key=\"${arr[0]}\">${arr[1]}</Label>`;\n})\nmsg.payload = output;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":1240,"wires":[["2274ba66c3d4e1ca"]]},{"id":"2274ba66c3d4e1ca","type":"debug","z":"d1395164b4eec73e","name":"debug 2453","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":1100,"wires":[]}]

```

Input

```auto
{
    "Unique1": "Test",
    "Unique2": "Test2"
}

```

Output

```auto
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Label Key="Unique1">Test</Label>
<Label Key="Unique2">Test2</Label>

```

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [6 November 2023 02:26 UTC](https://discourse.nodered.org/t/json-to-xml-correct-syntax/82630/4 "2023-11-06T02:26:59Z")

</div>

> [@jttjtt](#):
>
> I am always getting DuplicateKeyWarning in JSON editor in Node red and do not know how to write it correctly. Thanks for help.

Perhaps create that XML and pass it through the XML node to get the JSON?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [6 November 2023 07:18 UTC](https://discourse.nodered.org/t/json-to-xml-correct-syntax/82630/5 "2023-11-06T07:18:40Z")

</div>

> [@jttjtt](#):
>
> i am wondering how to write the JSON to get this XML:
> 
> ```auto
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <Label Key="Unique1">Test</Label>
> <Label Key="Unique2">Test2</Label>
> <Label Key="Unique3">Test3</Label>
> 
> ```
> 
> I am always getting DuplicateKeyWarning

It won't be possible to generate this using standard XML conversion techniques because that XML is actually invalid. It has multiple root nodes.

Are you 100% certain that XML is what you want?

---

<div class="post-metadata">

### Author: ![jttjtt](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@jttjtt](https://discourse.nodered.org/u/jttjtt)
#### Post date: [6 November 2023 07:35 UTC](https://discourse.nodered.org/t/json-to-xml-correct-syntax/82630/6 "2023-11-06T07:35:18Z")

</div>

I made some simplification, i am sorry.

It should look like this:

```auto
<?xml version="1.0" encoding="utf-8"?>
<Transfer Version="10">
  <ImportData>
                        <DataItem Key="MyKey1">Value1</DataItem>
                        <DataItem Key="MyKey2">Value 2</DataItem>
                        <DataItem Key="MyKey3">Value 3</DataItem>
            </ImportData>
<Transfer>

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [6 November 2023 08:16 UTC](https://discourse.nodered.org/t/json-to-xml-correct-syntax/82630/7 "2023-11-06T08:16:57Z")

</div>

Ok, that data is valid.

The easy way to do this is to pass this example XML data through an XML node and you will see the exact structure you need to create that XML.

Once you have the structure you can use that as a template and fill in the gaps with your data. Then Pass that through an XML node and you will get the XML generated in the format you require.

---

<div class="post-metadata">

### Author: ![jttjtt](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@jttjtt](https://discourse.nodered.org/u/jttjtt)
#### Post date: [6 November 2023 08:48 UTC](https://discourse.nodered.org/t/json-to-xml-correct-syntax/82630/8 "2023-11-06T08:48:53Z")

</div>

> [@jttjtt](#):
>
> ```auto
> <?xml version="1.0" encoding="utf-8"?>
> <Transfer Version="10">
> <ImportData>
> <DataItem Key="MyKey1">Value1</DataItem>
> <DataItem Key="MyKey2">Value 2</DataItem>
> <DataItem Key="MyKey3">Value 3</DataItem>
> </ImportData>
> <Transfer>
> 
> ```

So whis was a very good idea to use "reverse engineering"... So this is the right output:

````auto
{"Transfer":{"$":{"Version":"10"},"ImportData":[{"DataItem":[{"_":"Value1","$":{"Key":"MyKey1"}},{"_":"Value 2","$":{"Key":"MyKey2"}},{"_":"Value 3","$":{"Key":"MyKey3"}}]}]}}```
````

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [20 November 2023 08:49 UTC](https://discourse.nodered.org/t/json-to-xml-correct-syntax/82630/9 "2023-11-20T08:49:48Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
