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?
Asked
Active
Viewed 7,637 times
2 Answers
0
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 = '')

rivamarco
- 719
- 8
- 23
-1
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

TOTO
- 307
- 1
- 6
-
`head` from which package? Please add more information. – agent18 Dec 06 '20 at 23:40
-
`head` is a shell/Bash command in Unix systems, thus not Python. (there's also a `tail` command) you could invoke it with the `subprocess` python module to read from any ASCII file, e.g. `head -n 3 ~/my_project/samples.json` Where the `-n 3` is the number of lines to print, in this case three – Gergely M Sep 29 '22 at 12:19