Home > Enterprise >  How to keep missing item from being overwritten in JSON?
How to keep missing item from being overwritten in JSON?

Time:11-09

I have a file "stocks.json" that stores information about stock prices retrieved from an API. I wrote a Python function that takes in each symbol in the "stocks.json" file, adds each symbol to the API URL, and makes an API call with the URL. The function then takes whatever data is on the API and dumps it into the JSON file.

The problem right now is that when I add a symbol in the stocks.json file that isn't valid on the API, the API will not return any results for that specific symbol, and the function will overwrite the JSON file with whatever is on the API, thus wiping out that invalid symbol from the file. How do I make it so that the invalid symbol, stays in the JSON file?

Function:

def updateStocks(api_key):
   
    try:
       
        f = open('csv/stocks.json', 'r')
        all_stocks_settings = json.load(f)
        f.close()
        stock_info = all_stocks_settings['symbols']
        symbols = list(stock_info.keys())
     
   
        url = 'API URL'
        
        for symbol in symbols:
            url  = symbol   ','
            
        url  = '&apiKey='   api_key
        response = requests.get(url)
        data = response.json()
       
            
        stock_info = {}

        if len(data) > 0:
            for symbol in symbols:
                for stock in data:
                    if stock['symbol'] == symbol:
                        stock_info[stock['symbol']] = {'current': stock['price'], 'change': stock['change_since'], 'percent_change':stock['percent']}
                    
                    
            all_stocks_settings['symbols'] = stock_info
                
           
            f = open('csv/stocks.json', 'w ')
            json.dump(all_stocks_settings, f)
            f.close()
        
    except Exception as e:
        pass
 


stock_info (symbols inside the stocks.json file)

{"CHKP": {"current": "127.27", "change": "1.68", "percent_change": "1.34"}, "MSFT": {"current": "227.86", "change": "6.37", "percent_change": "2.88"}, "WMT": {"current": "142.52", "change": "1.53", "percent_change": "1.09"}, "HTZ": {"current": "17.90", "change": "0.36", "percent_change": "2.05"}, "NFLX": {"current": "257.01", "change": "-3.84", "percent_change": "-1.47"}, "GOOG": {"current": "88.69", "change": "2.00", "percent_change": "2.31"}, "AAPL": {"current": "138.25", "change": "-0.15", "percent_change": "-0.11"}, "ASDF":{}}

At the end of the list of stock_info, you see symbol "ASDF", which isn't a valid symbol on the API (using this only for example).

data (retrieved from the API based on stock_info)

[{"symbol":"CHKP","price":"127.27","close_price":"125.59","change_since":"1.68","percent":"1.34","updated":"11/07/22 15:01:02"},{"symbol":"MSFT","price":"227.86","close_price":"221.49","change_since":"6.37","percent":"2.88","updated":"11/07/22 15:01:03"},{"symbol":"WMT","price":"142.52","close_price":"140.99","change_since":"1.53","percent":"1.09","updated":"11/07/22 15:01:03"},{"symbol":"HTZ","price":"17.90","close_price":"17.54","change_since":"0.36","percent":"2.05","updated":"11/07/22 15:01:03"},{"symbol":"NFLX","price":"257.01","close_price":"260.85","change_since":"-3.84","percent":"-1.47","updated":"11/07/22 15:01:03"},{"symbol":"GOOG","price":"88.69","close_price":"86.69","change_since":"2.00","percent":"2.31","updated":"11/07/22 15:01:03"},{"symbol":"AAPL","price":"138.25","close_price":"138.40","change_since":"-0.15","percent":"-0.11","updated":"11/07/22 15:01:03"}]

As you can see, symbol "ASDF" from stock_info doesn't show up on the API's end.

When I run this function, it would overwrite and delete the symbol "ASDF" on stocks.json because it just takes the data from the API and dumps it on the JSON file. How do I make it so that symbol "ASDF" would stay in the JSON file? Even if "ASDF" doesn't show up on the API?

CodePudding user response:

data (retrieved from the API based on stock_info):

  • those data are arrays of object (where stock is object)

if i understand correctly this problem - that there isn't key - for identity, which stock is it?

in this arrays of objects - was transformed into dictionary (where key = symbol):


stocksWithoutKey = [{"symbol":"CHKP","price":"127.27","close_price":"125.59","change_since":"1.68","percent":"1.34","updated":"11/07/22 15:01:02"},{"symbol":"MSFT","price":"227.86","close_price":"221.49","change_since":"6.37","percent":"2.88","updated":"11/07/22 15:01:03"},{"symbol":"WMT","price":"142.52","close_price":"140.99","change_since":"1.53","percent":"1.09","updated":"11/07/22 15:01:03"},{"symbol":"HTZ","price":"17.90","close_price":"17.54","change_since":"0.36","percent":"2.05","updated":"11/07/22 15:01:03"},{"symbol":"NFLX","price":"257.01","close_price":"260.85","change_since":"-3.84","percent":"-1.47","updated":"11/07/22 15:01:03"},{"symbol":"GOOG","price":"88.69","close_price":"86.69","change_since":"2.00","percent":"2.31","updated":"11/07/22 15:01:03"},{"symbol":"AAPL","price":"138.25","close_price":"138.40","change_since":"-0.15","percent":"-0.11","updated":"11/07/22 15:01:03"}]
stocksWithKey=[]

for stock in stocksWithoutKey:
    vals = {}
    vals[stock['symbol']]=stock
    arr.append(vals)

output:

[{'CHKP': {'symbol': 'CHKP', 'price': '127.27', 'close_price': '125.59', 'change_since': '1.68', 'percent': '1.34', 'updated': '11/07/22 15:01:02'}}, {'MSFT': {'symbol': 'MSFT', 'price': '227.86', 'close_price': '221.49', 'change_since': '6.37', 'percent': '2.88', 'updated': '11/07/22 15:01:03'}}, {'WMT': {'symbol': 'WMT', 'price': '142.52', 'close_price': '140.99', 'change_since': '1.53', 'percent': '1.09', 'updated': '11/07/22 15:01:03'}}, {'HTZ': {'symbol': 'HTZ', 'price': '17.90', 'close_price': '17.54', 'change_since': '0.36', 'percent': '2.05', 'updated': '11/07/22 15:01:03'}}, {'NFLX': {'symbol': 'NFLX', 'price': '257.01', 'close_price': '260.85', 'change_since': '-3.84', 'percent': '-1.47', 'updated': '11/07/22 15:01:03'}}, {'GOOG': {'symbol': 'GOOG', 'price': '88.69', 'close_price': '86.69', 'change_since': '2.00', 'percent': '2.31', 'updated': '11/07/22 15:01:03'}}, {'AAPL': {'symbol': 'AAPL', 'price': '138.25', 'close_price': '138.40', 'change_since': '-0.15', 'percent': '-0.11', 'updated': '11/07/22 15:01:03'}}]

CodePudding user response:

This was fixed by removing stock_info = {}

  • Related