Deni Avinash
Deni Avinash

Reputation: 41

Printing top few lines of a large JSON file in Python

I have a JSON file whose size is about 5GB. I neither know how the JSON file is structured nor the name of roots in the file. I'm not able to load the file in the local machine because of its size So, I'll be working on high computational servers. I need to load the file in Python and print the first 'N' lines to understand the structure and Proceed further in data extraction. Is there a way in which we can load and print the first few lines of JSON in python?

Upvotes: 4

Views: 11002

Answers (3)

snakecharmerb
snakecharmerb

Reputation: 55913

There is no concept of "lines" in JSON data. A JSON file will consist of a single line unless the data has been formatted or it is a variant format like JSON lines.

Rather than reading a number of "lines" to get a feel for the data, read a number of characters instead:

with open('myfile.json') as f:
    sample = f.read(256)
print(sample)

Upvotes: 0

rivamarco
rivamarco

Reputation: 767

If you want to do it in Python, you can do this:

N = 3
with open("data.json") as f:
    for i in range(0, N):
        print(f.readline(), end = '')

Upvotes: 0

TOTO
TOTO

Reputation: 307

You can use the command head to display the N first line of the file. To get a sample of the json to know how is it structured. And use this sample to work on your data extraction.

Best regards

Upvotes: -1

Related Questions