registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register nowdef filewriter[line]: dictionary = {} dictionary['name'] = line[0][0] #assume this is a a string dictionary['item_to_buy'] = line[0][1] dictionary['currency'] = line[0][2] dictionary['league'] = line[0][3] with open['logs.json', 'r+'] as f: if len[f.read[]] == 0: f.write[json.dumps[dictionary]] else: f.write[',\n' + json.dumps[dictionary]] def retrieve[]: with open['logs.json'] as f: g = json.load[f] print[g[1]['name']]
To convert Python JSON string to Dictionary, use json.loads[] function. Note that only if the JSON content is a JSON Object, and when parsed using loads[] function, we get Python Dictionary object.
JSON content with array of objects will be converted to a Python list by loads[] function.
For example, following JSON content will be parsed to a Python Dictionary.
{"a":54, "b": 28}Following JSON content will be parsed to a Python List.
[{"a": 54, "b":28}, {"c": 87, "d":16}, {"e": 53, "f":74}]Example 1: JSON Object to Dictionary
In this example, we will take a JSON string that contains a JSON object. We will use loads[] function to load the JSON string into a Python Dictionary, and access the name:value pairs.
Python Program
import json jsonString = '{"a":54, "b": 28}' aDict = json.loads[jsonString] print[aDict] print[aDict['a']] print[aDict['b']] RunOutput
{'a': 54, 'b': 28} 54 28Example 2: JSON Nested Object to Dictionary
In this example, we will take a JSON string that contains a JSON object nested with another JSON object as value for one of the name:value pair. We will parse the JSON object to Dictionary, and access its values.
Python Program
import json jsonString = '{"a":54, "b": {"c":87}}' aDict = json.loads[jsonString] print[aDict] print[aDict['a']] print[aDict['b']] print[aDict['b']['c']] RunOutput
{'a': 54, 'b': {'c': 87}} 54 {'c': 87} 87Summary
In this tutorial of Python Examples, we learned how to convert a JSON Object string to a Python Dictionary, with the help of well detailed example programs.
JSON stands for JavaScript Object Notation. It means that a script [executable] file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json. To use this feature, we import the json package in Python script. The text in JSON is done through quoted-string which contains value in key-value mapping within { }. It is similar to the dictionary in Python.
Function Used:
-
json.load[]: json.loads[] function is present in python built-in ‘json’ module. This function is used to parse the JSON string.
Syntax: json.load[file_name]
Parameter: It takes JSON file as the parameter.
Return type: It returns the python dictionary object.
Example 1: Let’s suppose the JSON file looks like this:
We want to convert the content of this file to Python dictionary. Below is the implementation.
with open['data.json'] as json_file: data = json.load[json_file] print["Type:", type[data]] print["\nPeople1:", data['people1']] print["\nPeople2:", data['people2']] |
Output :
Example 2: Reading nested data In the above JSON file, there is a nested dictionary in the first key people1. Below is the implementation of reading nested data.
with open['data.json'] as json_file: data = json.load[json_file] print[data['people1'][0]] print["\nPrinting nested dictionary as a key-value pair\n"] for i in data['people1']: print["Name:", i['name']] print["Website:", i['website']] print["From:", i['from']] |
Output :
Article Tags :
In this article, we will discuss how to convert a list of dictionaries to JSON in python
Method 1: Using json.dumps[]
This function will convert a list of dictionaries to JSON.
Syntax:
json.dumps[dict, indent]Parameters:
- dictionary – name of a dictionary which should be converted to JSON object.
- indent – defines the number of units for indentation
Example: Python program to create a list of dictionaries of employee data and convert to JSON
data = [{"id": ["1", "2", "3"], "name": ["bhanu", "sivanagulu"], "department": ["HR", "IT"]}, {"id": ["4", "5", "6"], "name": ["sai", "poori"], "department": ["HR", "IT"]}, {"id": ["7", "8", "9"], "name": ["teja", "gowtam"], "department": ["finance", "IT"]}, {"id": ["10", "11", "12"], "name": ["sai", "jyothi"], "department": ["business", "IT"]}, {"id": ["13", "14", "15"], "name": ["prudhvi", "nagendram"], "department": ["business", "IT"]}] final = json.dumps[data, indent=2] |
Output:
[ { "id": [ "1", "2", "3" ], "name": [ "bhanu", "sivanagulu" ], "department": [ "HR", "IT" ] }, { "id": [ "4", "5", "6" ], "name": [ "sai", "poori" ], "department": [ "HR", "IT" ] }, { "id": [ "7", "8", "9" ], "name": [ "teja", "gowtam" ], "department": [ "finance", "IT" ] }, { "id": [ "10", "11", "12" ], "name": [ "sai", "jyothi" ], "department": [ "business", "IT" ] }, { "id": [ "13", "14", "15" ], "name": [ "prudhvi", "nagendram" ], "department": [ "business", "IT" ] } ]Method 2: Using json.dump[]
This will write converted JSON data into a file.
Syntax:
json.dump[dict, file_pointer]Parameters:
- dictionary – the name of dictionary which should be converted to JSON object.
- file pointer – pointer of the file opened in write or append mode.
Syntax:
with open["mydata.json", "w"] as final: json.dump[data, final]where mydata is the new JSON file. Finally, we have to download the created JSON file
Syntax:
files.download['mydata.json']Example:
from google.colab import files data = [{"id": ["1", "2", "3"], "name": ["bhanu", "sivanagulu"], "department": ["HR", "IT"]}, {"id": ["4", "5", "6"], "name": ["sai", "poori"], "department": ["HR", "IT"]}, {"id": ["7", "8", "9"], "name": ["teja", "gowtam"], "department": ["finance", "IT"]}, {"id": ["10", "11", "12"], "name": ["sai", "jyothi"], "department": ["business", "IT"]}, {"id": ["13", "14", "15"], "name": ["prudhvi", "nagendram"], "department": ["business", "IT"]}] with open["mydata.json", "w"] as final: files.download['mydata.json'] |
Output:
[{“id”: [“1”, “2”, “3”], “name”: [“bhanu”, “sivanagulu”], “department”: [“HR”, “IT”]}, {“id”: [“4”, “5”, “6”], “name”: [“sai”, “poori”], “department”: [“HR”, “IT”]}, {“id”: [“7”, “8”, “9”], “name”: [“teja”, “gowtam”], “department”: [“finance”, “IT”]}, {“id”: [“10”, “11”, “12”], “name”: [“sai”, “jyothi”], “department”: [“business”, “IT”]}, {“id”: [“13”, “14”, “15”], “name”: [“prudhvi”, “nagendram”], “department”: [“business”, “IT”]}]
Article Tags :