loading...

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

Collapse
kenbellows profile image
Ken Bellows

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.