Search code examples
pythonjsonbybit

Get specific data from API response using parameter


I'm unsure how to word the title of this question and no doubt there is an easy solution but I am a beginner.

I need to pass a parameter (symbol) to this function and have it return the 'minTradeQuantity'.

def get_exchange_minqtyize(symbol):
    return session_auth.query_symbol()['result']

I can retrieve the whole result response with the function like this, but if I add ['result'][0]['minTradeQuantity'] I can retrieve the 'minTradeQuantity' of the first name in the JSON response. How can I filter using the symbol parameter as 'name' to obtain the value I require?

The whole JSON response from the API docs is below:

{
"ret_code": 0,
"ret_msg": "",
"ext_code": null,
"ext_info": null,
"result": [
    {
        "name": "BTCUSDT",
        "alias": "BTCUSDT",
        "baseCurrency": "BTC",
        "quoteCurrency": "USDT",
        "basePrecision": "0.000001",
        "quotePrecision": "0.01",
        "minTradeQuantity": "0.0001",
        "minTradeAmount": "10",
        "minPricePrecision": "0.01",
        "maxTradeQuantity": "2",
        "maxTradeAmount": "200",
        "category": 1,
        "innovation": false,
        "showStatus": true
    },
    {
        "name": "ETHUSDT",
        "alias": "ETHUSDT",
        "baseCurrency": "ETH",
        "quoteCurrency": "USDT",
        "basePrecision": "0.0001",
        "quotePrecision": "0.01",
        "minTradeQuantity": "0.0001",
        "minTradeAmount": "10",
        "minPricePrecision": "0.01",
        "maxTradeQuantity": "2",
        "maxTradeAmount": "200",
        "category": 1,
        "innovation": false,
        "showStatus": true
    }
]

}


Solution

  • Go through the result & filter by symbol:

    def get_exchange_minqtyize(symbol):
        data = session_auth.query_symbol()['result']
        for sym_data in data:
            if sym_data['name'] == symbol:   # assuming the name here is the symbol you're looking for
                return sym_data['minTradeQuantity']