-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathjson.py
executable file
·48 lines (40 loc) · 1.01 KB
/
json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'''
Parsing JSON
------------
Python comes with a built-in module called 'json'. It contains the necessary
methods and functionality to parse & build JSON strings.
Read more at https://docs.python.org/3/library/json.html
'''
# the built-in module to handle json
import json
JSON_STRING = '''
{
"string": "hello world",
"boolean": true,
"null": null,
"numbers": 1234567,
"object": {
"key": "value",
"another_key": "another_value"
},
"arrays": [
"item_1",
"item_2",
"item_3"
]
}
'''
PYTHON_DICT = {
'arrays': ['item_1', 'item_2', 'item_3'],
'boolean': True,
'null': None,
'numbers': 1234567,
'object': {'another_key': 'another_value', 'key': 'value'},
'string': 'hello world'
}
# convert a json string to python object
converted_object = json.loads(JSON_STRING)
# the converted object is a dictionary
print(converted_object["string"])
# you can also convert structures back from python to json
converted_to_json = json.dumps(PYTHON_DICT)