-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_example.py
37 lines (32 loc) · 1.19 KB
/
json_example.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
# JSON or JavaScript Object Notation is a key value data format that is
# widely supported across a wide variety of languages. It is a convenient
# way to serialize and deserialize a nearly universal data exchange format
# that is most often used with
import json
def encode(data, indent = None):
return json.dumps(data, indent=indent)
def decode(data):
return json.loads(data)
if __name__ == "__main__":
# We can use a python dictionary directly to have syntax that looks
# close to JSON before we've even encoded anything
data = {
'firstName': 'Jane',
'lastName': 'Doe',
'age': 35,
'address': {
'street': '1234 Main Street',
'city': 'Salem',
'state': 'OR',
'zip': '97301'
},
'tags': ['pro', 'designer']
}
encoded = encode(data, indent=4)
print('Encoded value:\n{}'.format(encoded))
decoded = decode(encoded)
print('Decoded value: {}'.format(decoded))
print('We can access a few fields in the dictionary')
print('firstName: {}'.format(decoded['firstName']))
print('street: {}'.format(decoded['address']['street']))
print('2nd tag: {}'.format(decoded['tags'][1]))