Create JSON and XML in python

To represent data in JSON format 

name=John
age=20
gender=male
address=Sector 12 Greater Kailash, New Delhi
Jobs=Noida,Developer | Gurugram,Tester |Faridabad,Designer

info.json

 In json we repesent a data in key and value format and it is very easy. 

{
    "Key":"Value",
    "Key":"Value",  
    "Key":"Value",
    "Key":["Value","Value","Value"],  
    "Key":[
              {"Key":"Value","Key":"Value "},
              {"Key":"Value","Key":"Value"},
              {"Key":"Value","Key":"Value"}
          ]
}
  1.  In json if you are using Integer value then there is no need of ""  because  "" repesent strings. 
  2. In json if you are using one object than we can use it directly or in array format than it should be written like  ["array", array1, array2,...]
  3. In json if you are using multiple object then it should be under { }  like that {"Key":"Value","Key":"Value "}and you can add [ ] to represent as an array format
{
    "name":"john",
    "age":20,
    "gender":"male",
    "address":["New kP college","Greater Kailash","New Delhi"],
    "jobs":[
               {"Place":"Noida","Title":"Developer "},
               {"Place":"Gurugram","Title":"Tester "},
               {"Place":"Faridabad","Title":"Designer"}
           ]
}

To represent data in XML format 

info.xml

<!-- In xml we write a code under a key you can take any key -->
<info> <!-- key open -->

<name> john </name> 
<age> 20 </age>
<gender> male </gender>

<address> 
<item> New kP college </item>
<item> Greater Kailash </item>
<item> New Delhi </item>
</address>

<jobs>
 <item>
  <title>Developer </title>
  <place>Noida</place>
 </item>

 <item>
  <title>Designer</title>
  <place>Gurugram</place>
 </item>
 
 <item>
  <title>Developer </title>
  <place>Faridabad</place>
 </item>
</jobs>

</info> <!-- key close-->
​

 

Prepare data in Python:

To create JSON first you need to prepare data in python. We can use List and Dictionary in Python to prepare the data.

Python List  <==> JSON Array

Python Dictionary <==> JSON Object (Key Value Format)

According above notation let us prepare data and convert in JSON:

import json

data = {
    "name":"john", 
    "age":20, 
    "gender":"male", 
    "address":["New kP college","Greater Kailash","New Delhi"],
    "jobs":[
        {"Place":"Noida","Title":"Developer "},
        {"Place":"Gurugram","Title":"Tester "},
        {"Place":"Faridabad","Title":"Designer"}
    ]
}

jsonstring = json.dumps(data)

print(jsonstring)

 

In above example we used json.dump()  function to convert dictionary into JSON string. To use json library you need to import json in the top of page.

The output of above code is:

{"gender": "male", "age": 20, "jobs": [{"Place": "Noida", "Title": "Developer "}, {"Place": "Gurugram", "Title": "Tester "}, {"Place": "Faridabad", "Title": "Designer"}], "name": "john", "address": ["New kP college", "Greater Kailash", "New Delhi"]}

As you can see in Python JSON creation is very easy and like the Javascript.

 

Now see the methon to convert it into XML format:

from dicttoxml import dicttoxml

data = {
    "name":"john", 
    "age":20, 
    "gender":"male", 
    "address":["New kP college","Greater Kailash","New Delhi"],
    "jobs":[
        {"Place":"Noida","Title":"Developer "},
        {"Place":"Gurugram","Title":"Tester "},
        {"Place":"Faridabad","Title":"Designer"}
    ]
}

xml = dicttoxml(data, custom_root='info', attr_type=False)

print(xml)

To convert dictionary in XML you need to install dicttoxml library. You can use PIP to install it.

pip install dicttoxml

Now you can convert any data into XML format.

 



Keywords: