I am trying to convert a JSON object to XML in my Node js service with jstoxml module. My input structure is:
{
"user": "505723c5750c1fa2177682ed",
"uri": "http://localhost:3000/users/505723c5750c1fa2177682ed/items",
"items": [
{
"uri": "http://localhost:3000/items/1"
},
{
"uri": "http://localhost:3000/items/2"
}
],
"info": "blah."
}
My expect result is:
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<items>
<uri>http://localhost:3000/items/2</uri>
</items>
<info>blah.</info>
Result i am gettings is:
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
<uri>http://localhost:3000/items/2</uri>
</items>
<info>blah.</info>
If anyone faced similar issue, plese help me. Is there any other npm node package for getting this in expected structure?
Thanks
Disclaimer: I am the author of Jsonix, an open-source JavaScript library for XML<->JS conversion. Jsonix is available for Node.js (see here).
Jsonix can convert between XML and JSON (in both directions) based on a mapping. The mapping gives you the flexibility when transforming between JSON and XML. This may be what you need here.
I'll let the code speak. Here's a demo case for your JSON->XML conversion:
var Mapping = {
name : 'Mapping',
typeInfos : [ {
localName : 'Data',
propertyInfos : [ {
name : 'user'
}, {
name : 'uri'
}, {
name : 'items',
collection : true,
typeInfo : '.Item'
}, {
name : 'info'
} ]
}, {
localName : 'Item',
propertyInfos : [ {
name : 'uri'
} ]
} ],
elementInfos : [ {
elementName : {
localPart : 'data'
},
typeInfo : '.Data'
} ]
};
module.exports.Mapping = Mapping;
Here we have two types: the root type (I called it Data
) and another type Item
. The Data
type is used in the root XML element data
.
Ok, now the marshalling code:
// Create Jsonix context
var context = new Jsonix.Context([ Mapping ]);
var data = {
name : new Jsonix.XML.QName('data'),
value : {
"user" : "505723c5750c1fa2177682ed",
"uri" : "http://localhost:3000/users/505723c5750c1fa2177682ed/items",
"items" : [ {
"uri" : "http://localhost:3000/items/1"
}, {
"uri" : "http://localhost:3000/items/2"
} ],
"info" : "blah."
}
};
var marshaller = context.createMarshaller();
console.log(marshaller.marshalString(data));
And here's the XML you get:
<data>
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<items>
<uri>http://localhost:3000/items/2</uri>
</items>
<info>blah.</info>
</data>
Links:
If you can match your XML with your JSON, check other libraries, for instance xml2js.
I got solution for my question. Here you can get it. this is an online demo. By exporting code they provided we can achieve it,
Thanks for using my library! I apologize for the delay. You can achieve that output with this structure:
[
{"user": "505723c5750c1fa2177682ed"},
{"uri": "http://localhost:3000/users/505723c5750c1fa2177682ed/items"},
[
{"items": {"uri": "http://localhost:3000/items/1"}},
{"items": {"uri": "http://localhost:3000/items/1"}}
],
{"info": "blah."}
]
Output:
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<info>blah.</info>