1import json
2
3appDict = {
4 'name': 'messenger',
5 'playstore': True,
6 'company': 'Facebook',
7 'price': 100
8}
9app_json = json.dumps(appDict)
10print(app_json)
1import json
2# Data to be written
3dictionary ={
4 "A": 5,
5 "B": "guys",
6}
7
8# Serializing json
9json_object = json.dumps(dictionary, indent = 4)
10print(json_object)
11# Output
12{
13 "A": 5,
14 "B": "guys",
15}
1# app.py
2
3import json
4
5appDict = {
6 'name': 'messenger',
7 'playstore': True,
8 'company': 'Facebook',
9 'price': 100
10}
11app_json = json.dumps(appDict)
12print(app_json)
1import json
2
3json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
4'["foo", {"bar": ["baz", null, 1.0, 2]}]'
5
6print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True))
7{"a": 0, "b": 0, "c": 0}
8
1import json
2
3# with json.loads (string)
4info = '{"name": "Dave","City": "NY"}'
5res = json.loads(info)
6print(res)
7print("Datatype of the serialized JSON data : " + str(type(res)))
8#>>> {'name': 'Dave', 'City': 'NY'}
9#>>> Datatype of the serialized JSON data : <class 'dict'>
10
11# with json load (file)
12info = open('data.json',)
13res = json.load(info)
14print(res)
15print("Datatype after deserialization : " + str(type(res)))
16#>>> {'name': 'Dave', 'City': 'NY'}
17#>>> Datatype of the serialized JSON data : <class 'dict'>