-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_pointer.py
More file actions
43 lines (35 loc) · 1.12 KB
/
Copy pathjson_pointer.py
File metadata and controls
43 lines (35 loc) · 1.12 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
"""
file: json_pointer.py
description: demonstrates how to query a parsed json document using
rfc 6901 json pointer syntax.
"""
import cjsonx
def main():
json_data = '{"users": [{"id": 1, "profile": {"name": "bob"}}, {"id": 2, "profile": {"name": "alice"}}]}'
doc = cjsonx.parse(json_data)
if not doc.is_valid:
print("error parsing json")
return
root = doc.root
# query the first user's name using a json pointer
path1 = "/users/0/profile/name"
try:
name_node = root.pointer(path1)
print(f"pointer '{path1}' -> {name_node.get_str()}")
except Exception as e:
print(f"error querying pointer: {e}")
# query the second user's id
path2 = "/users/1/id"
try:
id_node = root.pointer(path2)
print(f"pointer '{path2}' -> {id_node.get_int()}")
except Exception as e:
print(f"error querying pointer: {e}")
# query a non-existent path
path3 = "/users/3/profile"
try:
root.pointer(path3)
except Exception as e:
print(f"pointer '{path3}' intentionally failed: {e}")
if __name__ == "__main__":
main()