-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample2_parse_stringify.py
More file actions
33 lines (29 loc) · 978 Bytes
/
example2_parse_stringify.py
File metadata and controls
33 lines (29 loc) · 978 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
29
30
31
32
33
from pprint import pprint
from jsonquerylang import parse, stringify
# parse the human friendly text format into the corresponding JSON format
text_query = '.friends | filter(.city == "new York") | sort(.age) | pick(.name, .age)'
json_query = parse(text_query)
pprint(json_query)
# ['pipe',
# ['get', 'friends'],
# ['filter', ['eq', ['get', 'city'], 'New York']],
# ['sort', ['get', 'age']],
# ['pick', ['get', 'name'], ['get', 'age']]]
# stringify the JSON format into the corresponding text format
print(stringify(json_query))
# """
# .friends
# | filter(.city == "new York")
# | sort(.age)
# | pick(.name, .age)
# """
# stringify with a custom indentation of 4 spaces
print(stringify(json_query, {"indentation": " "}))
# """
# .friends
# | filter(.city == "new York")
# | sort(.age)
# | pick(.name, .age)
# """
# now, both text_query and json_query can be used in the jsonquery() function,
# and json_query can be used in the compile() function.