-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjayson.py
More file actions
28 lines (23 loc) · 748 Bytes
/
jayson.py
File metadata and controls
28 lines (23 loc) · 748 Bytes
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
import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:, convert json to python
y = json.loads(x)
# the result is a Python dictionary:
print(y)
# Convert python to json
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
my_json = json.dumps(my_dict)
print(my_json)
print('........\n')
# Converting python objects of diff types e.g list,int,bool, e.t.c into JSON strings
print(json.dumps({'name': 'Ali', 'age': 30}))
print(json.dumps(['apples','dates']))
print(json.dumps(('apple','date')))
print(json.dumps('labaik'))
print(json.dumps(31))
print(json.dumps(11.890))
print(json.dumps(True))
print('........\n')
# Format results - indenting and ordering
print(json.dumps(my_dict,indent=4, sort_keys=True))