Array size using jsonpath expression - Stefan Goessner JsonPath
By : adam
Date : March 29 2020, 07:55 AM
should help you out It seems that support for returning the length() of an array was only added in version 2.1.0 of the jayway json-path library. Based on some quick tests, the $.orders.length() expression seems to work with both version 2.1.0 and version 2.2.0, so I think you just need to upgrade your dependency version in order to fix the error you are seeing.
|
Verify the existence of an array of root nodes with JSONpath
By : Tulunad Global
Date : March 29 2020, 07:55 AM
like below fixes the issue The root node operator is $ so you can just read $ into a map and that map will be keyed on your root node names. For example: code :
// Approach 1
Map<String, Object> read = JsonPath.read(json, "$");
assertThat(read.size(), is(2));
assertThat(read.keySet(), hasItem("tool"));
assertThat(read.keySet(), hasItem("book"));
// Approach 2: if you want a String[] then ...
String[] rootNodeNames = read.keySet().toArray(new String[read.size()]);
assertThat(rootNodeNames, Matchers.both(arrayWithSize(2)).and(arrayContainingInAnyOrder("book", "tool")));
// Approach 3: if you want to hide it all behind the hasJsonPath() matcher
// note: each of these assertion will cause your JSON string to be parsed
// so it's more efficient to do that once and assert against the
// resulting map as I did above
assertThat(json, hasJsonPath("$", Matchers.hasKey("tool")));
assertThat(json, hasJsonPath("$", Matchers.hasKey("book")));
|
JSONPath get an array element as JSON String
By : DKRON311
Date : March 29 2020, 07:55 AM
Any of those help As I mentioned in comments JsonPath will read a JSON object and return it like so. It will be like a HashMap with the types being defined by what was parsed. If you, as you mentioned, wants the values as a Json after extracting it you need to convert what was read back to a Json again: code :
public static void main(String[] args) {
String response = "{ \"book\": [ " +
" { \"category\": \"\"," +
" \"author\": \"Nigel Rees\"," +
" \"title\": \"Sayings of the Century\"," +
" \"price\": \"\"" +
" }," +
" { \"category\": \"fiction\"," +
" \"author\": \"\"," +
" \"title\": \"Sword of Honour\"," +
" \"price\": \"12.99\"" +
" }," +
" { \"category\": \"fiction\"," +
" \"author\": \"Herman Melville\"," +
" \"title\": \"Moby Dick\"," +
" \"isbn\": \"0-553-21311-3\"," +
" \"price\": \"8.99\"" +
" }," +
" { \"category\": \"fiction\"," +
" \"author\": \"J. R. R. Tolkien\"," +
" \"title\": \"The Lord of the Rings\"," +
" \"isbn\": \"0-395-19395-8\"," +
" \"price\": 22.99" +
" }" +
" ]" +
"}";
int length = JsonPath.read(response, "$.book.length()");
System.out.println(length);
Configuration conf = Configuration.defaultConfiguration();
Object document = Configuration.defaultConfiguration().jsonProvider().parse(response);
for (int i = 0; i < length; i++) {
String json = conf.jsonProvider().toJson(JsonPath.read(document, "$.book["+i+"]"));
System.out.println(json);
//process(json);
}
}
{"category":"","author":"Nigel Rees","title":"Sayings of the Century","price":""}
{"category":"fiction","author":"","title":"Sword of Honour","price":"12.99"}
{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":"8.99"}
{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}
|
JSONPath and Traverson filtering: single element array
By : user3052341
Date : December 15 2020, 09:21 AM
wish of those help Here is the Json I am trying to handle with Traverson to extract a single link from this HAL: , The code necessary to achieve what I should look like this: code :
JsonPathLinkDiscoverer disc = new JsonPathLinkDiscoverer("$._embedded..auth.._links..['%s']..href", MediaTypes.HAL_JSON);
Link authLink = disc.findLinksWithRel("auth:default", <json>).get(0);
|
How to find last element in Array list using jsonpath expression?
By : Deepak Grover
Date : March 29 2020, 07:55 AM
I wish this helpful for you As you can read in the docs you can ether do a
|