xxxxxxxxxx
import json
# json -> string
j1 = {"name": "steve", "age": 64}
s1 = json.dumps(j)
# string -> json
s2 = '{"name": "steve", "age": 64}'
j2 = json.loads(s)
xxxxxxxxxx
import json
appDict = {
'name': 'messenger',
'playstore': True,
'company': 'Facebook',
'price': 100
}
app_json = json.dumps(appDict)
print(app_json)
xxxxxxxxxx
import json
# JSON string
employee ='{"id":"09", "name": "Nitin", "department":"Finance"}'
# Convert string to Python dict
employee_dict = json.loads(employee)
print(employee_dict)
print(employee_dict['name'])
xxxxxxxxxx
import json
#save dictionary to json file
with open("/filename.json", 'w') as json_file:
json.dump(word_dict, json_file)
xxxxxxxxxx
import json
python_data = {'name': 'Dave', 'age': 42}
json_string = json.dumps(python_data)