Dictionary to JSON Python
- In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.
- We can use many built-in packages which arenamedJSON.
- Before using the packages, we have to import the package in python.
- Let us consider an example program:
# import thejson packing
import json
# Data to write
dictionary ={
"id": "02",
"name": "vijay",
"department": "Cr"
}
# Serializing json
json_obj = json.dumps(dictionary, indent = 2)
print(json_obj)
Output:
“id”: “02”,
“name”: “vijay”,
“department”: “CR”
Consider a program to convert a nested dictionary to JSON python:
# import thejson packing
import json
# data to be written
dictionary = {
"fruit":{"guava": "1","color": "green"},
"vegetable": {"tomato": "2","color": "red"},
}
# using the json
d = json.dumps(dictionary)
print(d)
Output:
"fruit":
{"guava": "1","color": "green"},
"vegetable":
{"tomato": "2","color": "red"}
- In the above program, a module is imported. And we consider two different variables, fruits, and vegetables, Which are considered dictionaries.
- A variable d is used. In which the json.dumps( ) function is used to store the dictionary.
Dictionary to JSON File Python
- Let us consider an example program to convert a dictionary to json file in python.
# importing json packing
import json
st = {"Name": "Kumari","Age": 16,"hobby":"reading"}
with open('student.json', 'w') as f:
json.dump(st, f)
Output:
“name”: “kumari”,
“age”: 16,
“hobby”: “reading”
- In the above program, we used a JSON module and used a student dictionary to open the file.
- The name of the file is student.json, and ‘w’ is used to write the file.
- The student.json file is stored in the dictionary the json.dump( ) module is used.
Converting Dictionary to JSON File in Python
- Let us consider an example program to convert dictionary to json file in python.
# import thejson packing
import json
class fruit(dict):
def __str__(self):
return json.dumps(self)
c = [[‘apple’,'banana']]
q= fruit(combo)
print(q)
Output:
“apple”: “orange”
- In the above code, the JSON module is used.
The method__str__(self) is used in the representation of the string.
- Here we also declared the c and q variables where q is assigned to the class.
- The parameter c is used to obtain the output.
Dictionary to JSON Array Python
- Let us consider an example program to convert a dictionary to a JSON array in python.
# import thejson packing
import json
dic = {'lion': 3, 'tiger': 1}
arr = [ {'key': i, 'value': dic[k]} for i in dic]
print(json.dumps(arr))
Output:
{ “key”: “ lion “, “value ”: 3},
{ “key”: “ tiger “, “value”: 1}
- In the above code, the JSON module is used.
- Here the dic is a variable with assigned key-value pair.
- The arr variable is used to declare the array, which can be used to check what is key and what is value.
- The file is stored in the dictionary, the json.dump( ) module is used.
- The loop is useful for storing of the value.
Multiple Dictionaries to JSON Python
- Let us consider an example program to convert multiple dictionary to json file in python.
# import the json packing
import json
dict = {"fruit":{"mango": "1","color": "yellow"},"vegetable": {"onion": "4","white": "red"},"chocolate":{"dairy milk":"2","color":"black"}}
report = json.dumps(dict)
print(report)
Output:
{"fruit":{"mango": "1","color”: "yellow"},"vegetable": {"onion": "4","white": "red"},"chocolate":{"dairy milk":"2","color":"black"}}
- In the above code, the JSON module is used. And a variable dict is declared.
- We used json.dumps ( ) module to store the data in the dictionary.
Dictionary to JSON Using the Sort_keys in Python
- Let us consider an example program to convert a dictionary to json file using sort_keysin python.
# import thejson packing
import json
dictionary ={"Name": "rushi", "Branch": " ECE ", "CGPA": " 8.0"}
d = json.dumps(dict, indent = 5, sort_keys = True)
print(d)
Output:
" Branch ": " ECE ",
" CGPA ": " 8.0"
" Name ": " rushi ",
- In the above code, the JSON module is used.
- Here the dic is a variable with assigned key-value pair.
- We used json.dumps module to store the values.
- The sort_keys is TRUE, which is used to sort the values in thedictionary. If the value is false then the values will not be sorted.