Skip to content
Advertisement

Accessing json value using jsonpath?

I’m following the tutorial for using jsonpath (https://www.baeldung.com/guide-to-jayway-jsonpath) and using the endpoint https://api.binance.com/api/v3/exchangeInfo I’m attempting to parse the value LOT_SIZE, stepSize for the symbol TRXEUR. The specific piece of JSON contained in the returned payload is contained in:

{
"symbol": "TRXEUR",
"status": "TRADING",
"baseAsset": "TRX",
"baseAssetPrecision": 8,
"quoteAsset": "EUR",
"quotePrecision": 8,
"quoteAssetPrecision": 8,
"baseCommissionPrecision": 8,
"quoteCommissionPrecision": 8,
"orderTypes": [
"LIMIT",
"LIMIT_MAKER",
"MARKET",
"STOP_LOSS_LIMIT",
"TAKE_PROFIT_LIMIT"
],
"icebergAllowed": true,
"ocoAllowed": true,
"quoteOrderQtyMarketAllowed": true,
"isSpotTradingAllowed": true,
"isMarginTradingAllowed": false,
"filters": [
{
"filterType": "PRICE_FILTER",
"minPrice": "0.00010000",
"maxPrice": "1000.00000000",
"tickSize": "0.00010000"
},
{
"filterType": "PERCENT_PRICE",
"multiplierUp": "5",
"multiplierDown": "0.2",
"avgPriceMins": 5
},
{
"filterType": "LOT_SIZE",
"minQty": "1.00000000",
"maxQty": "90000000.00000000",
"stepSize": "1.00000000"
},
{
"filterType": "MIN_NOTIONAL",
"minNotional": "10.00000000",
"applyToMarket": true,
"avgPriceMins": 5
},
{
"filterType": "ICEBERG_PARTS",
"limit": 10
},
{
"filterType": "MARKET_LOT_SIZE",
"minQty": "0.00000000",
"maxQty": "904859.10069444",
"stepSize": "0.00000000"
},
{
"filterType": "MAX_NUM_ORDERS",
"maxNumOrders": 200
},
{
"filterType": "MAX_NUM_ALGO_ORDERS",
"maxNumAlgoOrders": 5
}
],
"permissions": [
"SPOT"
]
}

More specifically how to extract 1.00000000 from :

{
"filterType": "LOT_SIZE",
"minQty": "1.00000000",
"maxQty": "90000000.00000000",
"stepSize": "1.00000000"
}

Here is what I’ve written :

public class ParseJson {

    public static void main(String[] args) {

        try {
            URL url = new URL("https://api.binance.com/api/v3/exchangeInfo");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer content = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            in.close();

            final String jsonString = content.toString();
            List<Object> dataObject = JsonPath.parse(jsonString).read("symbols");
            dataObject.forEach(x -> {
                        if (x.toString().toUpperCase().contains("TRXEUR")) {
                            List<Object> lo = JsonPath.parse(x.toString()).read("symbol");

                        }
                    }

            );
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

Which returns :

20:52:10.428 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['symbols']
20:52:10.469 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['symbol']
Exception in thread "main" com.jayway.jsonpath.PathNotFoundException: No results for path: $['symbol']
    at com.jayway.jsonpath.internal.path.EvaluationContextImpl.getValue(EvaluationContextImpl.java:133)
    at com.jayway.jsonpath.JsonPath.read(JsonPath.java:187)
    at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:102)
    at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:89)
    at com.reactive.api.scenarios.ParseJson.lambda$main$0(ParseJson.java:37)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
    at com.reactive.api.scenarios.ParseJson.main(ParseJson.java:34)

Process finished with exit code 1

I can access the symbol TRXEUR and symbols is parsed but how to extract 1.00000000 from :

{
"filterType": "LOT_SIZE",
"minQty": "1.00000000",
"maxQty": "90000000.00000000",
"stepSize": "1.00000000"
}

?

Advertisement

Answer

From https://jsonpath.herokuapp.com/

$.filters[?(@.filterType=='LOT_SIZE')].stepSize
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement