2

I am trying to use com.jayway.jsonpath.JsonPath to read key/values from a JSON string:

String contentAsString = mvcResult.getResponse().getContentAsString();
System.out.println(contentAsString);
Object value = JsonPath.read(contentAsString, "$.key");

But I get the error:

Expected to find an object with property ['key'] in path $ but found 'net.minidev.json.JSONArray'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.

Printing contentAsString gives:

[{"firstName":"N","key":"mykey"}]

Is contentAsString not valid JSON?

0

1 Answer 1

3

The blob that you posted is not a valid JSON Object, however, it IS a valid JSON Array.

To read this using JsonPath, try using this:

Object value = JsonPath.read(contentAsString, "$[0].key");

This will get the value of the key object, from the 0th element of the initial array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.