JSON to XML correct syntax

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

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

I get this XML:

<?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:

<?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.

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

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

I would probably just write a function and a simple object

[{"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

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

Output

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

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

1 Like

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?

I made some simplification, i am sorry.

It should look like this:

<?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>

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.

1 Like

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

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

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