1
  1. I want to get the last element in a JSON file
  2. This file is very large, so I do not want to load it into memory (using json.loads)
  3. 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

2 Answers2

0

This should do:

import ijson

with open('customerAccounts.json', 'rb') as file:
  for key, value in ijson.kvitems(file, ''):
    pass
  print(key, value)

key and value are here the last values from the iteration process. There are other ways to get the latest value from an iteration that might be faster (e.g., using a deque of size 1), but this should do.

Rodrigo Tobar
  • 569
  • 4
  • 13
0

I hope this would be helpful

with open('output.txt', 'r') as f:
    print(f.read().splitlines()[-2])

In this way you aren't waste time to parse json. after getting last line, you can parse your data. If I show it here:

{
"1000045901": "John",
"1000045941": "Johns",
"1000045921": "Johnsd", # => -3
"1000045902": "Ben" # => -2
} # => -1

credit: Same question

Farshid Shekari
  • 2,391
  • 4
  • 27
  • 47