/
SquadTestJSONPath.py
executable file
·72 lines (57 loc) · 2.39 KB
/
SquadTestJSONPath.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
#
# for single user:
# pip install jsonpath-rw jsonpath-rw-ext --user
# for global:
# sudo -H pip install jsonpath-rw jsonpath-rw-ext
#
# OR preferred way through virtualenv
# virtualenv jsonpath
# source jsonpath/bin/activate
# pip install jsonpath-rw jsonpath-rw-ext
#
import json
from jsonpath_rw import jsonpath
#from jsonpath_rw import parse
# override parse for more capabilities
from jsonpath_rw_ext import parse
import jsonpath_rw_ext as jp
# does work, but extension makes it easier to just use 'match' and 'match1'
# so this goes unused now
def showJSONValues( json_data, expr ):
jsonpath_expr = parse(expr)
for match in jsonpath_expr.find(json_data):
print(match.value)
return
####### MAIN ##########################################################3
# read file from disk
json_file = open("squad.json")
json_data=json.load(json_file)
# show simple attribute, then values from array
print("Squad: {}".format( jp.match1("squadName",json_data) ) )
print("\nMembers:")
for name in jp.match("$.members[*].name",json_data):
print(" {}".format(name))
# get all members, count length of returned list
print("\nCount members in list: {}".format( len(jp.match("$.members[*]",json_data )) ))
# use extensions to provide direct count of number of members in array
print("Count members using len extension: {}".format( jp.match1("$.members.`len`",json_data ) ))
# lookup array element given simple embedded element
lookFor="Madame Uppercut"
print("\nPowers of {}".format(lookFor))
powers = jp.match1("members[?name='" + lookFor + "'].powers",json_data)
for power in powers:
print(" {} has the power of {}".format(lookFor,power))
# find only array items that have element
print("\nAliases?")
memberHasAliases=jp.match("members[?(aliases)]",json_data)
for member in memberHasAliases:
print("{} has aliases: {}".format( member['name'],member['aliases'] ))
# find only array items where embedded structure has matching word
print("\nDoes anyone have an alias that contains 'Red'?")
memberHasAliases=jp.match("members[?(aliases[*]~'.*Red.*')]",json_data)
for member in memberHasAliases:
print("{} has alias that contains 'Red', {}".format( member['name'],member['aliases'] ))
# find nested array items that contain word
print("\nWhich specific aliases contain the word 'Red'?")
for thisalias in jp.match("members[*].aliases[?(@~'.*Red.*')]",json_data):
print(" Alias that contains 'Red': {}".format( thisalias ))