-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorking with JSON .py
133 lines (74 loc) · 1.55 KB
/
Working with 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import json
# In[48]:
people_string =''' {
"people": [{
"name": "John",
"age": 30,
"email": "jon@gmail.com",
"cars": {
"car1": "Ford",
"car2": "BMW",
"car3": "Honda"
},
"children": {
"c1": "Jack",
"c2": "Jill"
},
"phone": "202-456-3214"
},
{
"name": "Tami",
"age": 28,
"email": "tami@gmail.com",
"cars": {
"car1": "Tesla"
},
"children": {
"c1": "Cylia",
"c2": "Honey"
},
"phone": "232-478-4514"
}
]
}
'''
# In[49]:
print(type(people_string))
# In[50]:
type(people_string)
# In[51]:
data = json.loads(people_string)
# In[52]:
for person in data['people']:
print(person['name']," ",end='')
print(person['children'])
# In[53]:
for contact in data['people']:
print(contact['name'],contact['email'],contact['phone'])
# In[55]:
#tidying in json format
tidy = json.dumps(data,indent=3,sort_keys=True)
print(tidy)
# # Loading Data in a file
# In[88]:
with open('people.json','w') as f:
for contact in data['people']:
f.write(contact['name'] + " " + contact['email'] + " " + contact['phone'])
f.close()
# # Reading data from a file
# In[78]:
with open('/Users/amodpanchal/Desktop/States.json') as f:
data1=json.load(f)
#print(data1)
tidy_data = json.dumps(data1,indent=2)
print(tidy_data)
# In[95]:
for states in data1['states']:
print(states['name'],states['abbreviation'])
# In[97]:
for states in data1['states']:
print(states['abbreviation'])
# In[ ]: