Wednesday, 10 May 2017

Convert JSON to XML 'x' elements in array

I have a JSON array, which can contain several different elements in it - the number is unknown to me.

JSON:

[{
    "_id": "5911a43e6aa5d609d32c590a",
    "criteria": "3",
    "name": "test1"
}, {
    "_id": "5911a43e6aa5d609d32c590d",
    "criteria": "7",
    "name": "test2"
}, {
    "_id": "5911a43e6aa5d609d32c5910",
    "criteria": "2",
    "name": "test3"
}]

I need to convert this data into XML, so that it is in the following structure:

<MyDefs criteria="3">
    <name>test1</name>
</MyDefs>
<MyDefs criteria="7">
    <name>test3</name>
</MyDefs>
<MyDefs criteria="2">
    <name>test2</name>
</MyDefs>

I tried using xml2js node library, however I am struggling with the number of elements- I assume I need a foreach loop but not sure how to implement - is there a way to achieve this?

Thank you



via emisqwe

No comments:

Post a Comment