I am currently working with Java, reading JSON response from a webservice. Till now I have been parsing JSON for a path known in advance. So I am able to make objects and arrays depending on situation.
String jsonText = readAll(br);
JSONObject json = new JSONObject(jsonText);
JSONObject resp = json.getJSONObject("Response");
But now I have a problem. I have to ask user to provide me a path and I have to get the value at that path in JSON response. Path could be incorrect - return error in that case.
Kind of like XPath in XML. Do we have something similar in JSON?
Path could for example look like: /Response/VehicleSearch/Vehicles/Vehicle[2]/Features/Feature[7]/ID
Please excuse me if its a stupid question. Any help is appreciated. Thanks in advance.
For JSON equivalent of XPATH for XML you can use JsonPath
As per docs:
JsonPathis toJSONwhatXPATHis toXML, a simple way to extract parts of a given document.JsonPathis available in many programming languages such asJavascript,PythonandPHP. Now also in Java!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With