0

all. I'm having a really strange issue with https://github.com/json-path/JsonPath

One of the issues seems to be a re-entrant issue that the implentation has: when executing a path, each piece returns a string:

Expected to find an object with property ['ModuleListResponse'] in path $[0]['response'] but found 'java.lang.String'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.

I've kind of "hacked" that by passing a JSONObject/JSONArray to JsonPath.read() instead of a JSON string. After doing that, now I'm getting:

Filter: [0]['name'] can only be applied to arrays. Current context is: [{"startLocation":{"types":["city"],"address":"Argentina","latitude":-34.6075682,"name":"Buenos Aires","description":"Buenos Aires is the capital and largest city of Argentina. The city is located on the western shore of the estuary of the Río de la Plata, o...449a049a781"}}]

As you can see, that's already an array. I've been googling a lot, but can't find what the issue is.

Regarding the code that's parsing it, there you have it:

    jsonString = StringUtils.trim(jsonString);
    if (jsonString.startsWith("[")) {
        jsonObject = new org.json.JSONArray(jsonString);
    } else {
        jsonObject = new JSONObject(jsonString);
    }
    String jsonPath = "$[0].name";
    Object jsonResult = JsonPath.using(conf)
                             .parse(jsonObject)
                             .read(jsonPath);

So, the question is: why JsonPath is reading the json as string, instead of json? And for the second issue, why it's not taking it as array, when it is clearly an array.

gaspo53
  • 103
  • 2
  • 9
  • Please [edit] your post to include a question you want to ask. – Progman Dec 17 '19 at 14:13
  • Thanks, Progman! Did it – gaspo53 Dec 17 '19 at 16:48
  • 1
    Please [edit] your question to include your full source code you have as a [mcve], which can be compiled and tested by others. – Progman Dec 17 '19 at 16:54
  • What do you try to receive? JSONPath queries a JSONtree by a query syntax. Where is your query? Do you look for a lib like jackson? – nologin Dec 17 '19 at 16:58
  • What is the `jsonPath`? It needs to be prefixed with `$.` to start at the root. Your `if` statement is unnecessary and overly complex. `JsonPath#read` is a generic method that will return whatever the `lvalue` is. You can also pass in a class, e.g. `.read("$.[0].name", String.class)`. – Christopher Schneider Dec 17 '19 at 17:01
  • Added jsonPath definition. What I'm trying to get is a valid json, but it doesn't get even there, it fails in the middle: "Expected to find an object with property ['ModuleListResponse'] in path $[0]['response'] but found 'java.lang.String'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'." – gaspo53 Dec 17 '19 at 18:36

1 Answers1

0

If you format your JSON to something easily readable, you'll see that your property path is incorrect. Here's the actual JSON:

[
  {
  "startLocation": 
    {
      "types": ["city"],
      "address": "Argentina",
      "latitude": -34.6075682,
      "name": "Buenos Aires",
      "description": "Buenos Aires is the capital and largest city of Argentina. The city is located on the western shore of the estuary of the Río de la Plata, o...449a049a781"
    }
  }
]

You are trying to access $.[0].name, but that value doesn't exist.

$.[0] is

{
    "startLocation": {
      "types": ["city"],
      "address": "Argentina",
      "latitude": -34.6075682,
      "name": "Buenos Aires",
      "description": "Buenos Aires is the capital and largest city of Argentina. The city is located on the western shore of the estuary of the Río de la Plata, o...449a049a781"
}

The only top level key of $.[0] is startLocation, so what you're actually looking for is

$.[0].startLocation.name

I'd recommend undoing the JSONArray and JSONObject fluff you added, as it's possible that masked the real issue, which was simply an invalid property path.

Christopher Schneider
  • 3,745
  • 2
  • 24
  • 38