0

I have an expression in JsonPath which outputs me something like the following:

[  
   "someString"
]

however, this causes my Java code to crash, because I cannot assign it directly to a string. Yes, I could get the first position of the Array in Java, but it would be nicer to directly become this from JsonPath:

{  
   "someString"
}

I have tried something like {jsonPathExpression}[0], but then I get an empty array... Am I missing something?

Here a Json Sample Code:

{  
   "products":[  
      [  
         "foo",
         "bar",
         "notthisone",
         "456",
         "789",
         "000"
      ],
      [  
         "foo",
         "bar",
         "bingo",
         "456",
         "!!!!",
         "000"
      ]
   ]
}

and the path: $.products[?(@[2] == 'bingo')][4]

And please don´t blame the ugly json structure ([] braces after products instead of {}), as you can imagine, it is a remote service which I need to call and want only the necessary data... ;)

Martin
  • 3,018
  • 1
  • 26
  • 45
  • That's not possible in JsonPath, From [the docs](http://goessner.net/articles/JsonPath/): "Please note, that the return value of jsonPath is an array, which is also a valid JSON structure. So you might want to apply jsonPath to the resulting structure again or use one of your favorite array methods as sort with it." – glytching Jan 31 '19 at 15:46
  • @glytching, thank you for the info. I now wrote it this way: `JsonPath.read(JsonPath.read(input, "$.products[?(@[2] == 'bingo')][4]").toString(), "$[0]")`. This is ugly, but still looks a bit better than evaluating the JSONArray with Java... ;) – Martin Jan 31 '19 at 16:37

0 Answers0