Accessing JSON Value using JSONPath?

Question

Source: https://stackoverflow.com/questions/67451735/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 extract1.00000000 from:

{

"filterType":"LOT_SIZE",

"minQty":"1.00000000",

"maxQty":"90000000.00000000",

"stepSize":"1.00000000"

}

Here is what I've written :

publicclassParseJson{

 

publicstaticvoidmain(String[]args){

 

try{

URLurl=newURL("https://api.binance.com/api/v3/exchangeInfo");

HttpURLConnectioncon=(HttpURLConnection)url.openConnection();

con.setRequestMethod("GET");

BufferedReaderin=newBufferedReader(newInputStreamReader(con.getInputStream()));

StringinputLine;

StringBuffercontent=newStringBuffer();

while((inputLine=in.readLine())!=null){

content.append(inputLine);

}

in.close();

 

finalStringjsonString=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(IOExceptione){

e.printStackTrace();

}

 

 

}

}

Which returns :

20:52:10.428[main]DEBUGcom.jayway.jsonpath.internal.path.CompiledPath-Evaluatingpath:$['symbols']

20:52:10.469[main]DEBUGcom.jayway.jsonpath.internal.path.CompiledPath-Evaluatingpath:$['symbol']

Exceptioninthread"main"com.jayway.jsonpath.PathNotFoundException:Noresultsforpath:$['symbol']

atcom.jayway.jsonpath.internal.path.EvaluationContextImpl.getValue(EvaluationContextImpl.java:133)

atcom.jayway.jsonpath.JsonPath.read(JsonPath.java:187)

atcom.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:102)

atcom.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:89)

atcom.reactive.api.scenarios.ParseJson.lambda$main$0(ParseJson.java:37)

atjava.base/java.util.ArrayList.forEach(ArrayList.java:1540)

atcom.reactive.api.scenarios.ParseJson.main(ParseJson.java:34)

 

Processfinishedwithexitcode1

 

I can access the symbol TRXEUR and symbols is parsed but how to extract1.00000000 from:

{

"filterType":"LOT_SIZE",

"minQty":"1.00000000",

"maxQty":"90000000.00000000",

"stepSize":"1.00000000"

}

?

Answer

Your job is to find the value of stepSize where the symbols value is TRXEUR and filterType value is LOT_SIZE. But the code will be long and complicated if you use Java to do it.

Suggest you using SPL, an open-source Java package. Coding will be really easy and you only need one line:

A

1

=json(httpfile("https://api.binance.com/api/v3/exchangeInfo").read()).symbols.select(symbol=="TRXEUR").filters.select(~.filterType=="LOT_SIZE").stepSize

SPL offers JDBC driver to be invoked by Java. Just store the above SPL script as getvalue.splx and invoke it in a Java application as you call a stored procedure:

Class.forName("com.esproc.jdbc.InternalDriver");

con= DriverManager.getConnection("jdbc:esproc:local://");

st = con.prepareCall("call getvalue()");
st.execute();

View SPL source code.