I'm using ijson.kvitems to iterate over all of the keys in a JSON file that I have.
the JSON file looks like this:
{"filename":{"file_data":
{"name":"samplefile",
"filetype":"Miscellaneous",
"id":123,
"timestamp":"2020-10-08 00:20:00"}}}
based on this answer, a simplified version of my code looks something like so (v is a dictionary too):
import ijson
f = open('file.json')
for k, v in ijson.kvitems(f, ''):
name = v['name']
user_id = v['id']
filetype = v['filetype']
timestamp = v['timestamp']
I am only able to stream/read about 94% of the keys from the original file this way, trying to figure out if there is a way to get to the remaining 6%.
Thanks!!
The documentation for
kvitemsmaybe isn't fully clear: it returns key/value pairs at the given prefix, and it's not recursive. With your example document and code this is whatkvitemsreturns (note that as of writingijson.dumpisn't yet on the latest PyPI ijson release, but is available on the latestmasterversion on GitHub):Here
keyisfilename, whilevalueis the rest of the object, since that whole object is the value underfilename. In particular keys likenameorfiletypewill not be reported separately; if you wanted those (and their respective values) to be reported you'd have to use afilename.file_dataprefix instead.From the comments in the original question I'm guessing this is the actual problem, but couldn't add this more extensive comment here to further clarify things, and with the hopes it's also the actual answer to your problem.