Home > Blockchain >  How can I set a condition for a JSON file?
How can I set a condition for a JSON file?

Time:12-08

I'm trying to write a program that will read a JSON file and take the information I need from it. In my JSON file there is a parameter: 'exchangeId' which outputs the exchange number. I would like to make sure that only those exchanges that I set, or rather their 'exchangeId', are recorded in my dictionary, and if coin don't have any 'exchangeId' at the list this coin don't wanna save as JSON file. How can I do this?

My code:

response = s.get(url=url, headers=headers).json()
json_data = response['data']
pairs = []
out_object = {}

for pair in json_data["marketPairs"]:
    pairs.append({
        "exchange_name": pair["exchangeName"],
        "market_url": pair["marketUrl"],
        "price": pair["price"],
        "last_update" : pair["lastUpdated"],
        "exchange_id": pair["exchangeId"] # That ids what I'm would to have
                                          # 102,311,200,302,521,433,482,406,42,400
        })

out_object["name_of_coin"] = json_data["name"]
out_object["marketPairs"] = pairs
out_object["pairs"] = json_data["numMarketPairs"]

JSON file:

{
    "name_of_coin": "yearn.finance",
    "marketPairs": [
        {
            "exchange_name": "Binance",
            "market_url": "https://www.binance.com/en/trade/YFI_USDT",
            "price": 24087.947333328204,
            "last_update": "2021-12-07T17:49:54.000Z",
            "exchange_id": 270
        },
        {
            "exchange_name": "FTX",
            "market_url": "https://ftx.com/trade/YFI/USD",
            "price": 24110.0,
            "last_update": "2021-12-07T17:49:53.000Z",
            "exchange_id": 524
        },
        {
            "exchange_name": "Coinbase Exchange",
            "market_url": "https://pro.coinbase.com/trade/YFI-USD",
            "price": 24085.37,
            "last_update": "2021-12-07T17:48:57.000Z",
            "exchange_id": 89
        },
        {
            "exchange_name": "Huobi Global",
            "market_url": "https://www.huobi.com/en-us/exchange/yfi_usdt",
            "price": 24096.582498139305,
            "last_update": "2021-12-07T17:49:53.000Z",
            "exchange_id": 102
        },
        {
            "exchange_name": "Bybit",
            "market_url": "https://www.bybit.com/en-US/trade/spot/YFI/USDT",
            "price": 24045.551976080656,
            "last_update": "2021-12-07T17:48:10.000Z",
            "exchange_id": 521
        },
        {
            "exchange_name": "FTX",
            "market_url": "https://ftx.com/trade/YFI/USDT",
            "price": 24079.402222379562,
            "last_update": "2021-12-07T17:49:53.000Z",
            "exchange_id": 524
        },
        {
            "exchange_name": "Binance",
            "market_url": "https://www.binance.com/en/trade/YFI_BTC",
            "price": 24090.344540422568,
            "last_update": "2021-12-07T17:49:54.000Z",
            "exchange_id": 270
        },
        {
            "exchange_name": "Bithumb",
            "market_url": "https://www.bithumb.com/trade/order/YFI_KRW",
            "price": 25285.64198822739,
            "last_update": "2021-12-07T17:49:53.000Z",
            "exchange_id": 200
        },
        {
            "exchange_name": "Gemini",
            "market_url": "https://gemini.com/",
            "price": 24088.85,
            "last_update": "2021-12-07T17:49:55.000Z",
            "exchange_id": 151
        },
        {
            "exchange_name": "Bitfinex",
            "market_url": "https://www.bitfinex.com/t/YFI:USD",
            "price": 24117.0,
            "last_update": "2021-12-07T17:49:54.000Z",
            "exchange_id": 37
        },
        {
            "exchange_name": "Binance",
            "market_url": "https://www.binance.com/en/trade/YFI_BUSD",
            "price": 24061.58763433993,
            "last_update": "2021-12-07T17:49:54.000Z",
            "exchange_id": 270
        },
        {
            "exchange_name": "Coinbase Exchange",
            "market_url": "https://pro.coinbase.com/trade/YFI-BTC",
            "price": 24020.347144833217,
            "last_update": "2021-12-07T17:48:57.000Z",
            "exchange_id": 89
        },
        {
            "exchange_name": "FTX US",
            "market_url": "https://ftx.us/trade/YFI/USD",
            "price": 23985.0,
            "last_update": "2021-12-07T17:48:07.000Z",
            "exchange_id": 1177
        },
        {
            "exchange_name": "Binance",
            "market_url": "https://www.binance.com/en/trade/YFI_EUR",
            "price": 24018.992856877743,
            "last_update": "2021-12-07T17:49:54.000Z",
            "exchange_id": 270
        },
        {
            "exchange_name": "Bitfinex",
            "market_url": "https://www.bitfinex.com/t/YFI:UST",
            "price": 24036.37648809482,
            "last_update": "2021-12-07T17:49:54.000Z",
            "exchange_id": 37
        }
     }
]

CodePudding user response:

To only get information for the exchanges ids you want, just check for them when creating the pairs list. Since the ids are hashable, the fastest way to do that in Python is by storing the ids in a set which makes checking for membership extremely fast.

pairs = []
desired_ids = {102,311,200,302,521,433,482,406,42,400}  # Exchange ids

for pair in json_data["marketPairs"]:
    if pair["exchangeId"] in desired_ids:
        pairs.append({
            "exchange_name": pair["exchangeName"],
            "market_url": pair["marketUrl"],
            "price": pair["price"],
            "last_update" : pair["lastUpdated"],
            "exchange_id": pair["exchangeId"]
            })

Suggestion

You could simplify the conversion process and make it more "data-driven" by using a dictionary display (or dictionary comprehension as they are more commonly called) to create the ones with renamed keys that get appended to the pairs list.

pairs = []
desired_ids = {102,311,200,302,521,433,482,406,42,400}  # Exchange ids
oldkeys = 'exchangeName', 'marketUrl', 'price', 'lastUpdated', 'exchangeId'
newkeys = 'exchange_name', 'market_url', 'price', 'last_update', 'exchange_id'

for pair in json_data["marketPairs"]:
    if pair["exchangeId"] in desired_ids:
        pairs.append({newkey: pair[oldkey] for oldkey, newkey in zip(oldkeys, newkeys)})

CodePudding user response:

exchanges = [102,311,200,302,521,433,482,406,42,400]

for pair in json_data["marketPairs"]:
    #Condition like Ronald Das Mentioned 
    if pair["exchangeId"] in exchanges:
            pairs.append({
                "exchange_name": pair["exchangeName"],
                "market_url": pair["marketUrl"],
                "price": pair["price"],
                "last_update" : pair["lastUpdated"],
                "exchange_id": pair["exchangeId"] #That ids what I'm would to have 102,311,200,302,521,433,482,406,42,400
                                
             })
  • Related