-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON_func.py
More file actions
52 lines (42 loc) · 1.19 KB
/
JSON_func.py
File metadata and controls
52 lines (42 loc) · 1.19 KB
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
# python Json methods
# Here's a table showing Python objects and their equivalent conversion to JSON.
# Python JSON Equivalent
# dict object
# list, tuple array
# str string
# int, float, int number
# True true
# False false
# None null
# Note: the creating json is attaching list dict or string to dict
import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y["age"])
print("=================================================1")
x = {"name": "John",
"age": 30,
"city": "New York"}
# convert into JSON:
# use four indents to make it easier to read the result:
y=json.dumps(x,indent=3)
print(y)
print("=================================================2")
# Python read JSON file
# with open('path_to_file/person.json', 'r') as f:
# data = json.load(f)
# Output: {'name': 'Bob', 'languages': ['English', 'French']}
# print(data)
print("=================================================3")
# Writing JSON to a file
person_dict = {"name": "Bob",
"languages": ["English", "French"],
"married": True,
"age": 32
}
with open('person.txt', 'w') as json_file:
json.dump(person_dict, json_file)
print(person_dict)