XML Converter · Tutorial
How to convert an XML API response to JSON
6 min readbeginnerUpdated
A supplier’s API returns orders as XML, but your app works with JSON. You will convert the response, choose options that make the JSON easy to use in code, and check the shape.
What you need
- The XML Converter set to XML → JSON
- A sample XML response
Steps
Paste the response
Load the example: two orders with attributes, a currency on each total and one or two items each.
Read the shape
orders.orderis an array of two objects. Attributes appear as@idand@status, and each total becomes{"@currency": "EUR", "#text": 59.8}.Make arrays consistent
The second order has one item, so
itemis an object there but an array in the first. Tick Always use arrays so code can always loop overitem.for (const order of data.orders[0].order) { console.log(order['@id'], order.item.length); }Use real numbers
Keep Numbers and true/false ticked so
4200andtruearrive as a number and a boolean, not strings.
Try it yourself
Opens the XML Converter with this tutorial's sample loaded.
Common mistakes
- Relying on the shape of one sample
- A sample with one item hides the fact that items repeat. Use Always use arrays or a larger sample.
- Converting very large feeds in production code
- For production, use a streaming XML parser in your language. This tool is ideal for exploring data and building test fixtures.
Next steps
Go back to the XML Converter, or keep going with one of these: