Skip to content

Conversion between Data Exchange Formats

JSON to XML - Example 1

{
    "menu": {
        "id": "file",
        "value": "File",
        "popup": {
            "menuitem": [{
                    "value": "New",
                    "onclick": "CreateNewDoc()"
                },
                {
                    "value": "Open",
                    "onclick": "openDoc()"
                },
                {
                    "value": "Close",
                    "onclick": "CloseDoc()"
                }
            ]
        }
    }
}
The equivalent XML conversion for the above JSON content is :

<?xml version="1.0" encoding="UTF-8"?>
<menu>
    <id>file</id>
    <value>File</value>
    <popup>
        <menuitem>
            <value>New</value>
            <onclick>CreateNewDoc()</onclick>
        </menuitem>
        <menuitem>
            <value>Open</value>
            <onclick>openDoc()</onclick>
        </menuitem>
        <menuitem>
            <value>Close</value>
            <onclick>CloseDoc()</onclick>
        </menuitem>
    </popup>
</menu>

XML to JSON - Example 2

<?xml version="1.0" encoding="UTF-8"?>
<employees>
    <employee>
        <firstname>John</firstname>
        <lastname>Doe</lastname>
        <age>30</age>
    </employee>
    <employee>
        <firstname>Anna</firstname>
        <lastname>Smith</lastname>
        <age>25</age>
    </employee>
    <employee>
        <firstname>Peter</firstname>
        <lastname>Jones</lastname>
        <age>35</age>
    </employee>
</employees>
The equivalent JSON conversion for the above XML content is :

{
    "employees":[
        {
            "firstname":"John",
            "lastname":"Doe",
            "age":30
        },
        {
            "firstname":"Anna",
            "lastname":"Smith",
            "age":25
        },
        {
            "firstname":"Peter",
            "lastname":"Jones",
            "age":35
        }
    ]
}

XML to JSON - Example 3

This example includes the XML content with the list of order_line.

<?xml version="1.0" encoding="UTF-8"?>
<order id="1234" data="234234">
    <customer first_name="John" last_name="Doe">
        <email>[email protected]</email>
        <phone>123456789</phone>
    </customer>
    <content>
        <order_line item="H252" quantity="2">
            <unit_price>10.00</unit_price>
        </order_line>
        <order_line item="H253" quantity="1">
            <unit_price>20.00</unit_price>
        </order_line>
    </content>
    <credit_card number="1234123412341234" expiry="12/12" cvv="123" type="VISA" />
</order>
The equivalent JSON conversion for the above XML content is :

{
    "order": {
        "@id": "1234",
        "@data": "234234",
        "customer": {
            "@first_name": "John",
            "@last_name": "Doe",
            "email": "[email protected]",
            "phone": "123456789"
        },
        "content": {
            "order_lines": [{
                "@item": "H252",
                "@quantity": "2",
                "unit_price": 10.00
            }, {
                "@item": "H253",
                "@quantity": "1",
                "unit_price": 20.00
            }]
        },
        "credit_card": {
            "@number": "1234123412341234",
            "@expiry": "12/12",
            "@cvv": "123",
            "@type": "Visa"

        }
    }
}

JSON to XML - Example 4

{
    "country": [{
            "name": "Nepal",
            "capital": "Kathmandu",
            "feature": "Everest"
        },
        {
            "name": "India",
            "capital": "New Delhi",
            "feature": "Taj Mahal"
        },
        {
            "name": "China",
            "capital": "Beijing",
            "feature": "Great Wall"
        }
    ]
}
The equivalent XML conversion for the above JSON content is :

<?xml version="1.0" encoding="UTF-8"?>
<countries>
    <country>
        <name>Nepal</name>
        <capital>Kathmandu</capital>
        <feature>Everest</feature>
    </country>
    <country>
        <name>India</name>
        <capital>New Delhi</capital>
        <feature>Taj Mahal</feature>
    </country>
    <country>
        <name>China</name>
        <capital>Beijing</capital>
        <feature>Great Wall</feature>
    </country>
</countries>