Parsing JSON In Python

What is JSON? How JSON is useful in Automation?

JSON stands for JavaScript Object Notation. It is easy to read and flexible text file which is a popular format used for representing the structured data. It is commonly used to receive and transmit data between server and web application in JSON format.

Mainly JSON file could be used for passing the configuration details in a framework. So you need to update only one file in order to run your automation framework.

Format of JSON object:
{
    "firstname": "Roshni",
    "lastname": "Tiwari",
    "age": 29,
}
To Work with JSON, you need to import the json module of python. json module makes it easy to parse json strings and files containing json objects.
import json
There are three methods which are frequently used in json parsing

  1. json.dumps()
  2. json.dump()
  3. json.loads()
  4. json.load()

Lets see each one with examples
1. Read JSON file
import json
with open("exampleJson.json", "r") as fp:
    obj = json.load(fp)
print(obj)
  1. exampleJson.json is the json file which I want to read.
  2. We need to open the json file in read mode ("r" stands here for read mode)
  3. json.load() method would load the data of json file to a variable.
  4. Later we have print the variable
Output would be as below:
{u'lastname': u'Tiwari', u'age': u'29', u'firstname': u'Roshni'}
Process finished with exit code 0

2. Write to a JSON file
import json
details_dicts = {"firstname" : "Roshni", "lastname" : "Tiwari", "Known languages": ["Hindi","English","Marathi"],"State":"Maharshtra"}
with open("model.json", "w") as fp:
    json.dump(details_dicts,fp)

  1. Here, we have taken a dictonary string which we would be writing in a json file.
  2. If model.json file already exist, it will overwrite else it will create a new file with model.json in a writing mode.
  3. json.dump() method would dump the dictionary string to a json file.

Output would be as below:
{"lastname": "Tiwari", "State": "Maharshtra", "firstname": "Roshni", "Known languages": ["Hindi", "English", "Marathi"]}
3. Access JSON data
import json
with open("exampleJson.json", "r") as fp:
    obj = json.load(fp)
    firstname = obj["firstname"]
    print firstname

  1. exampleJson.json  open in  read mode.
  2. load the data of json in obj variable using json.load() method
  3. To access the firstname, we need to give the key "firstname" and corresponding value would be printed.

Output of above program would be as below:
Roshni

4. Convert dictionary to JSON
import json
obj = json.loads("""{        "firstName": "Roshni",        "lastName": "Tiwari",        "age": 35        }""")
obj_list = json.dumps(obj)
print obj_list
  1. json.dumps() method is used for converting the dictionary to json.
output would be as below:
{"lastName": "Tiwari", "age": 35, "firstName": "Roshni"}

5. JSON to Dictionary Conversion
import json
per_data = '{"firstname": "Roshni", "languages": ["English", "Hindi","Marathi"]}'per_dict = json.loads(per_data)
print(per_data)
print(per_dict['languages'])

  1. per_data is the json string here
  2. json.loads() method is loading the json string to variable per_dict
  3. If we see the variable per_data, its a dictionary now
  4. To access the data from dictionary, we are passing the key and its corresponding values would be displayed on screen.

Output would be as below:
{"firstname": "Roshni", "languages": ["English", "Hindi","Marathi"]}
[u'English', u'Hindi', u'Marathi']




Comments