Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Large JSON file
#1
Hi All,

I build a program to read a JSON file from internet.
When the file is large , the python program hangs and I have to shut it down then run it again and it hangs again.
The programs works well with small JSON files.

import urllib.request, json

myURL = "https://query1.finance.yahoo.com/v8/finance/chart/AAPL?symbol=INTC&period1=0&period2=9999999999&interval=1d"
with urllib.request.urlopen(myURL) as url:
    data = json.loads(url.read().decode())
    print(data)
any idea how to overcome this issue?

--Update.
I found that the program run file with the print(data)
it seems printing is causing a problem that I don't know !
Reply
#2
I guess you had some network issues. Try it again.

Later you may want to save the data.
Requesting data over and over again when testing, takes too much time.
Some providers limit their APIs with x requests per minute.
from urllib.request import urlopen
import json
import sys
import pickle

myURL = "https://query1.finance.yahoo.com/v8/finance/chart/AAPL?symbol=INTC&period1=0&period2=9999999999&interval=1d"
timeout = 5
try:
    with urlopen(myURL, timeout=timeout) as request:
        data = request.read().decode()
except Exception as e:
    print('Unable to request data from internet')
    sys.exit(1)

try:
    data = json.loads(data)
except Exception as e:
    print("Can't parse json string")
    sys.exit(1)
else:
    print('Finished loading json')


with open('finance.yahoo.com.pickle', 'wb') as fd:
    pickle.dump(data, fd)

with open('finance.yahoo.com.json', 'w') as fd:
    json.dump(data, fd)
On my side it works. I get the data and the data can parsed with json.loads.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
I think the problem is in printing data !

I modified your code a little bit
else:
    print('Finished loading json') 
    print(data)
The program freeze after I added print(data)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 191 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Parsing large JSON josvink66 5 627 Jan-10-2024, 05:46 PM
Last Post: snippsat
  parse json field from csv file lebossejames 4 725 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Python Script to convert Json to CSV file chvsnarayana 8 2,496 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,097 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Converted EXE file size is too large Rajasekaran 0 1,508 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,400 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,264 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Writing to json file ebolisa 1 996 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Trying to parse only 3 key values from json file cubangt 8 3,447 Jul-16-2022, 02:05 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020