json-path / JsonPath

Java JsonPath implementation
Apache License 2.0
8.96k stars 1.65k forks source link

Parser fails when value contains " '' " #493

Open javierpajaro opened 6 years ago

javierpajaro commented 6 years ago

Parser fails when trying to parse the following content.

{"fieldDataCollection":[{"id":"CL1:COM","commodityUnits":"USD/bbl.","commodityContractDate":"Sep 2018","name":"Generic 1st 'CL' Future","price":67.37,"priceChange1Day":0.17,"percentChange1Day":0.2529760003089905,"lastUpdateEpoch":"1534197994","lowPrice":67.36,"highPrice":67.43,"priceDate":"8/13/2018","priceTime":"6:06 PM","lastUpdateTime":"6:06 PM","lastUpdateISO":"2018-08-13T22:06:34.000Z","userTimeZone":"EDT","longName":"WTI Crude Oil (Nymex)","shortName":"WTI Crude"},{"id":"CO1:COM","commodityUnits":"USD/bbl.","commodityContractDate":"Oct 2018","name":"Generic 1st 'CO' Future","price":72.61,"priceChange1Day":-0.2,"percentChange1Day":-0.2746880054473877,"lastUpdateEpoch":"1534197598","lowPrice":71.04,"highPrice":73.09,"priceDate":"8/13/2018","priceTime":"5:59 PM","lastUpdateTime":"5:59 PM","lastUpdateISO":"2018-08-13T21:59:58.000Z","userTimeZone":"EDT","longName":"Brent Crude (ICE)","shortName":"Brent Crude"},{"id":"CP1:COM","commodityUnits":"JPY/kl","commodityContractDate":"Jan 2019","name":"Generic 1st 'CP' Future","price":48450,"priceChange1Day":140,"percentChange1Day":0.28979501128196716,"lastUpdateEpoch":"1534192201","lowPrice":47420,"highPrice":48620,"priceDate":"8/13/2018","priceTime":"4:30 PM","lastUpdateTime":"4:30 PM","lastUpdateISO":"2018-08-13T20:30:01.000Z","userTimeZone":"EDT","longName":"Crude Oil (Tokyo)","shortName":"Crude Oil (Tokyo)"},{"id":"NG1:COM","commodityUnits":"USD/MMBtu","commodityContractDate":"Sep 2018","name":"Generic 1st 'NG' Future","price":2.925,"priceChange1Day":-0.005,"percentChange1Day":-0.170647993683815,"lastUpdateEpoch":"1534197901","lowPrice":2.923,"highPrice":2.926,"priceDate":"8/13/2018","priceTime":"6:05 PM","lastUpdateTime":"6:05 PM","lastUpdateISO":"2018-08-13T22:05:01.000Z","userTimeZone":"EDT","longName":"Natural Gas (Nymex)","shortName":"Natural Gas (Nymex)"}]}

javierpajaro commented 6 years ago

If I remove ' characters enclosing CL it works.....it should be nice to tell the parser that ' is not a delimiter

BTW I am using the default json provider.

hf-kklein commented 6 years ago

Could you boil it down to minimal (not) working example?

hezonghan commented 3 years ago

Hello! I tested your json with the following code:

String json_str = "your long json";
String path_str = "$.fieldDataCollection";
List<Object> cd2 = JsonPath.read(json_str , path_str);
for(Object obj : cd2) System.out.println(obj);

using the newest forked code. The single quotes are printed normally.

a1exeyz commented 1 year ago

How can I compile a path with a single quote in the name? The following example throws an exception:

 final String path = String.format("$.['%s']", "Homer's hammer");
 JsonPath.compile(path);