2

I'm trying to get a specific array item not by the array position, but by the content key.

Example JSON:

{"items": [{
        "key": "prop-name",
        "label": "Provider",
        "type": "DROPDOWN",
        "items": ["bla", "blub", "what", "ever"]
    }, {
        "key": "prop-modes",
        "label": "Transport modes",
        "type": "CHECKBOX",
        "items": ["AIR", "RAIL", "ROAD", "SEA"]
    }
]}
private static String URL_GLOBAL_FILTER = "/global-filter";
private static String PROP_NAME = "prop-name";

mvc.perform(get(URL_GLOBAL_FILTER).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
   .andExpect(jsonPath("$.items", hasSize(10)))
   .andExpect(jsonPath("$.items[?((@.key === \"" + PROP_NAME + "\" && @.type === \"DROPDOWN\"))]").exists())
   .andExpect(jsonPath("$.items[0].items", hasSize(4)));

This works perfectly fine. But what I want to do is something like:

.andExpect(jsonPath("$.items[?(@.key == \"prop-name\")].items", containsString("bla")))

But that throws the following error:

java.lang.AssertionError: JSON path "$.items[?(@.key == "prop-name")].items"

Expected: a string containing "bla" but: was a net.minidev.json.JSONArray (<[["bla","blub","what","ever"]]>)

What does the < > mean? How to access the array inside?

In jsonQueryTool it seems to be what I want.

(UPDATED question)

1 Answer 1

3

Have you tried using the hasItem matcher from Hamcrest?

import static org.hamcrest.Matchers.*;

.andExpect(jsonPath("$.items[?(@.key == 'prop-name')].items[*]", hasItem("bla")));
Sign up to request clarification or add additional context in comments.

1 Comment

* is a wildcard, items[*] means selecting all elements of the items array

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.