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:
2. Write to a JSON file
Output of above program would be as below:
4. Convert dictionary to JSON
5. JSON to Dictionary Conversion
Output would be as below:
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 jsonThere are three methods which are frequently used in json parsing
- json.dumps()
- json.dump()
- json.loads()
- json.load()
1. Read JSON file
import json with open("exampleJson.json", "r") as fp: obj = json.load(fp) print(obj)
- exampleJson.json is the json file which I want to read.
- We need to open the json file in read mode ("r" stands here for read mode)
- json.load() method would load the data of json file to a variable.
- Later we have print the variable
{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)
- Here, we have taken a dictonary string which we would be writing in a json file.
- If model.json file already exist, it will overwrite else it will create a new file with model.json in a writing mode.
- json.dump() method would dump the dictionary string to a json file.
{"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
- exampleJson.json open in read mode.
- load the data of json in obj variable using json.load() method
- 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
- json.dumps() method is used for converting the dictionary to json.
{"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'])
- per_data is the json string here
- json.loads() method is loading the json string to variable per_dict
- If we see the variable per_data, its a dictionary now
- 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
Post a Comment