Synkied
Synkied

Reputation: 171

Python nested dictionary add key:values from json

This is my first question on StackOverflow. I searched throughly as always (that's why it's my first question ever !) but can't seem to get my head around what seems to be a simple problem...

What I'm doing:

My problem :

I would like to build a dictionary like so :

{Market:{FROM/TO: price, FROM/TO: price, ...}, {Market:{FROM/TO: price, FROM/TO: price, ...}, ...}

to use it as a dataframe afterwards.

But what I get from my actual code is a dictionary like that :

{Market:{FROM/TO: price}, {Market:{FROM/TO: price}}

Where 'FROM/TO: price' is the last 'Key:value' sent by the API (in my example code, it's ETH/USD, when it exists for a market).

I would like my code to "see" where a "Market" key already exists in the dictionary, and to append all new "FROM/TO: price" pairs to this "Market" key.

I know that something's wrong with my code (obviously), but I really can't get my head around it...

My actual code:

import requests
import json

d = {}
s = []
def get_data_from_url(*args):
"""
Get datas from an API url.
Treat it as json and filter everything not needed.
"""
for symbol_pair in args:
    for symbols in symbol_pair:
        from_symbol, to_symbol = symbols
        url = 'https://www.cryptocompare.com/api/data/coinsnapshot/?fsym=' \
            + from_symbol + '&tsym=' + to_symbol
        resp = requests.get(url=url)
        datas = json.loads(resp.text)

        # filter to get only the exchanges data
        exchanges_data = datas["Data"]["Exchanges"]

        for data_dict in exchanges_data:
            if data_dict["TOSYMBOL"] == "WUSD":
                data_dict["TOSYMBOL"] = "USD"
            # if the exchange is not outdated
            if data_dict["VOLUME24HOUR"] != "0":
                # s.append([data_dict["MARKET"], {data_dict["FROMSYMBOL"] + "/" + data_dict["TOSYMBOL"]: data_dict["PRICE"]}])
                d[data_dict["MARKET"]] = {}

                d[data_dict["MARKET"]].update({data_dict["FROMSYMBOL"] + "/" + data_dict["TOSYMBOL"]: data_dict["PRICE"]})
print(d)

args = [("BTC", "USD"), ("ETH", "USD")]

get_data_from_url(args)

Could you please help me solve this problem ? I would be really grateful !

Thanks everyone and keep it nice Pythonistas :)

Upvotes: 0

Views: 463

Answers (2)

Acccumulation
Acccumulation

Reputation: 3591

Every time you update d[data_dict["MARKET"]], you first set it to {}. So of course it only contains the last piece of data; all the others were deleted when the next piece of data came along. You should look for an existing dictionary and take an empty dictionary as default only if it doesn't already exist. For example:

 d[data_dict["MARKET"]] = d.get([data_dict["MARKET"],{}).update({data_dict["FROMSYMBOL"] + "/" + data_dict["TOSYMBOL"]: data_dict["PRICE"]})

Upvotes: 1

v100ev
v100ev

Reputation: 186

Ok, I think I got your problem. It's pretty simple - You were creating a dict and updating it each time. You should check if it exists first and then update. And if it doesn't exist - create it with it's first element. Something like this (it works):

            # if the exchange is not outdated
            if data_dict["VOLUME24HOUR"] != "0":
                if d.get(data_dict["MARKET"]):
                    d[data_dict["MARKET"]].update({data_dict["FROMSYMBOL"] + "/" + data_dict["TOSYMBOL"]: data_dict["PRICE"]})
                else:
                    d[data_dict["MARKET"]]= {data_dict["FROMSYMBOL"] + "/" + data_dict["TOSYMBOL"]: data_dict["PRICE"]}

Upvotes: 1

Related Questions