xml-js
xml-js copied to clipboard
remove _attributes object maintaining its value
is it possibile to format the js object in a way that the _attributes object has only its value? from something like this
{ _attributes: { name: 'titolo', description: 'il titolo della sezione' }}
to something like this
{ name: 'titolo', description: 'il titolo della sezione' }
I can say that based off the README, no. Those keys are reserved for elements (
I actually tried to do that too but could not do it via the library, therefore i used post-cleaning of the result like below.
//parse xml-js result (which is a json string), so we can convert it from json string to json object var convertedXml = JSON.parse(result); //use traverse function to add the _attributes' properties into the _attributes Object itself and then delete the _attributes Object convertedXml = traverse_it(convertedXml); function traverse_it(obj: any) { for (var prop in obj) { if (typeof obj[prop] == 'object') { if (obj[prop]._attributes) { Object.assign(obj[prop], obj[prop]._attributes); delete obj[prop]._attributes; } traverse_it(obj[prop]); } } return obj; }
If you need the result as a string, then you can use JSON.stringify(convertedXml);