- I want to get the last element in a JSON file
- This file is very large, so I do not want to load it into memory (using json.loads)
- Happy to use something like ijson or jsonparser, but can't figure out how to do it with these parsers.
So let's say my JSON file looks something like this:
{
"1000045901": "John",
"1000045902": "Ben"
}
I want to write python code that helps me get the last item ("1000045902": "Ben") -- including it's key & its value -- all without loading the file into memory.
The closest I've come to this is using ijson:
import ijson
key = '-'
with open('customerAccounts.json', 'rb') as file:
for prefix, event, value in ijson.parse(file):
if prefix == '' and event == 'end_map'
print ([key, value][-1])
My output however returns the end_map value i.e. None