简体   繁体   中英

Rest assured - How to validate JSON response field with the same name

I have the sample JSON response

     "userinfo": [
            {
                "userName": "name1",
                "registered": false
            },
            {
                "userName": "name2",
                "registered": true
            }
          ]

How can I verify when userName = name1 then registered = false When userName = name2 then registered = true

Tried something like this but it does not work.

.body("$.findAll{it.userinfo.userName = name1"}.registered", equalTo(false));

Did I miss something or do something wrong?

Here's something that can help you out,

    String json = "{\r\n" + "   \"userinfo\": [{\r\n" + "           \"userName\": \"name1\",\r\n"
            + "         \"registered\": false\r\n" + "      },\r\n" + "     {\r\n"
            + "         \"userName\": \"name2\",\r\n" + "           \"registered\": true\r\n" + "       }\r\n"
            + " ]\r\n" + "}";

    JsonPath js = new JsonPath(json);
    String emp1Name = js.get("userinfo.find {it.userName == 'name2'}.registered").toString();
    System.out.println(emp1Name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM