2

Suppose I have the following json:

{
  "page": 2,
  "per_page": 3,
  "total": 12,
  "total_pages": 4,
  "data": [
 {
    "id": 4,
    "first_name": "Eve",
    "last_name": "Holt",
    "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
},
{
    "id": 5,
    "first_name": "Charles",
    "last_name": "Morris",
    "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
},
{
    "id": 6,
    "first_name": "Tracey",
    "last_name": "Ramos",
    "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
}]}

I would like to extract only the value of "first_name" which belongs to id = 6.

I can overcome this with something else, but I want to know if there is a straight way with restassured

Something I have tried so far: data.find { it.id == '6' }.first_name

is it possible? If true, how to do that? Thanks.

6
  • If course it's possible. Have you made any attempt to do so on your own? Commented May 21, 2019 at 11:30
  • Sure. no success. I can overcome this with something else, but I want to know if there is a straight way with restassured. Commented May 21, 2019 at 11:33
  • Please show what you have tried. Commented May 21, 2019 at 11:39
  • data.find { it.id == '6' }.first_name Commented May 21, 2019 at 11:46
  • Please edit your question with that information. Part of the etiquette on SO is showing your work and asking complete standalone questions. Please don't take my comments as anything but an attempt to help you improve your question. Commented May 21, 2019 at 11:49

2 Answers 2

2

I am not sure if this solution will be what you are looking for but you can definitely extract the first name based on the id.

Use JsonPath library in Rest Assured. You can either pass JSON as a String JsonPath.from("json string") or you can get it from Response like this:

JsonPath path = response.jsonPath();

Then, you can get data array and iterate over its elements like this:

    List<HashMap<String, Object>> data = path.getList("data");
    for (HashMap<String, Object> singleObject : data) {
        if (singleObject.get("id").equals(6)) {
            System.out.println(singleObject.get("first_name"));
        }
    }

The above will print name "Tracey" to the console. You can either work with this code or create a method to return parameter based on id like this:

    private static String getFirstNameById(JsonPath path, int id) {
        List<HashMap<String, Object>> data = path.getList("data");
        for (HashMap<String, Object> singleObject : data) {
            if (singleObject.get("id").equals(id)) {
                return (String) singleObject.get("first_name");
            }
        }
        throw new NoSuchElementException(String.format("Can't find first_name with id: %d", id));
    }

Just to make sure you understand - each of the element in data array is JSON Object. We collector all of the objects with method getList. Each of the object is represented by HashMap of String and Object because it contains different types. first_name is String and id is Integer, that's why we have to use HashMap of Objects as second generic argument.

Then we iterate over the HashMap, comparing id of each JSON Object to get desired results.

You can also use Java Stream if you'd like. The same method would look like this:

List<HashMap<String, Object>> data = JsonPath.from(json).getList("data");
Optional<String> firstName = data.stream().filter(x -> x.get("id").equals(6)).map(x -> (String) x.get("first_name")).findFirst();
if (firstName.isPresent()) {
    System.out.println(firstName.get());
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use below JsonPath, it will give you first_name based on id:

[?(@.id ==4)].first_name

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.