I have multiple JSON files to cover and can't seem to access the specific text(distracter) below. This is an example of a line in the file :
{"extracted":"high","nameid":3201932,"users":{"name":[{"ids":[28,37],"text":"distracter"}],"symbols":[]}}
Below is the code that I wrote that returns an empty result:
data = []
with open(fileName, 'r') as file_to_read:
for line in file_to_read:
data.append(json.loads(line))
json_tree = objectpath.Tree(data)
text_result= tuple(json_tree.execute('$.users.name[@.text]'))
return text_result
I think there are two main problems here:
'$.users.name.text'
and found that worked for me (using Python3 and objectpath)Try something like this instead:
In the loop above we build up a list of names, rather than decoded entities. In your version the
text_result
variable is repeatedly instantiated and only the last one is returned.You might also be able to increase the speed by using a pure python approach to getting the data.
The first is careful about not raising errors with missing data, but if you know your data is always the right shape, you could try the second.
In my testing they are 15x faster (for the careful version) and 20x faster for the careless version.