Python - Append to a Json file in a loop

Viewed 2305

Good morning

Yesterday i started to write a simple announcment bot for cryptocurrency markets. At this point i would like to read the ticker (prices of a market) and save the data into .json file in real time. Python's dump function does the trick. So far the code looks like this

start = time.time()
while 1:
    end = time.time()
    raz = end - start
    if raz >= 5:
        for i in range(len(self.keys)):
             self.dictTicker(self.values[i])
             start = end
    sleep(0.005)

self.dictTicker is the function that goes through selected markets and saves data in .json file

def dictTicker(self, market):
    ticker = api.get_ticker(market)
    self.data['year'] = datetime.now().year
    self.data['month'] = datetime.now().month
    self.data['day'] = datetime.now().day
    self.data['hour'] = datetime.now().hour
    self.data['min'] = datetime.now().minute
    self.data['sec'] = datetime.now().second
    self.data['bid'] = ticker['result']['Bid']
    self.data['ask'] = ticker['result']['Ask']
    self.data['last'] = ticker['result']['Last']

    with open("{}.json".format(market), 'a') as f:
        json.dump([self.data], f)
        f.close()

An example of .json file then looks like this.

{"year": 2017, "month": 8, "day": 6, "hour": 6, "min": 38, "sec": 4, "bid": 0.00224, "ask": 0.00225999, "last": 0.00225999}{"year": 2017, "month": 8, "day": 6, "hour": 6, "min": 38, "sec": 9, "bid": 0.00223611, "ask": 0.00224, "last": 0.00224}

The problem is that this format {}{} is wrong. It should be {{}{}}. Or in other words list of dictionaries and not multiple dictionaries.

When i try to read the file i use the following code:

 with open("BTC-SEC.json") as f:
     a = json.load(f)
     print(a)

I get the following error:

json.decoder.JSONDecodeError: Extra data: line 1 column 121 (char 120)

I have searched on stack but i cant find the answer that would help me. Any help would be greatly appreciated.

1 Answers

I had the same problem and I figured out after some time that the trick is to have in fact a list of dictionaries so [{},{}]. So it's just about creating an empty list before the loop and then .extend() it at each iteration. Finally, outside the loop, the classic json.dump(extended_list, file) does a great job!
Hope it helped

Related