DEV Community

Discussion on: How can I convert data from XML to JSON format?

Collapse
 
kenbellows profile image
Ken Bellows • Edited

Couple thoughts. First, you may not need to; JavaScript has solid native support for dealing with XML documents. If you want to check that out, start with this MDN article on dealing with XML in JavaScript.

But if you really need to translate to JSON (or just want to, because let's be real, JSON is way easier to work with than XML), then the question is how you want to do it. XML has an extra feature that JSON doesn't have: attributes. You have to decide how you want to distinguish between element contents and attributes when you convert to JSON.

David Walsh has an old blog post demonstrating his personal JavaScript function for converting XML to JSON. It uses a pretty popular method, which is to add a special property at each level that has attributes called "@attributes" and put the attribute values in there.

Collapse
 
murugan_s profile image
Murugan • Edited

Hi @kenbellows I have tried the xml to json conversion using DOM parser in nodejs. After parsing it I have tried to get the get the node using document.getElementsByTagName.
But this is not working when my tag name is not in small case.
For eg: if my tagname is "Userprofile"

Can you please help me with this?