'How to get value from json using restassured

I have json and there is array of 'products'.

{
  "data": [
    {
      "contact_id": "...",
      "email": "...",
      "first_name": "...",
      "hash_campaign": "",
      "hash_campaign_valid_to_date": null,
      "hash_password": "",
      "last_name": "...",
      "loans": [
        
      ],
      "max_available_loan_amount": 2000.00,
      "middle_name": "P",
      "mobile": "...",
      "personal_id": "...",
      "product_code": 2,
      "products": [
        {
          "available_loan_terms": [
            15,
            21,
            30,
            7,
            45
          ],
          "default_amount": 0,
          "default_term": 15,
          "document_required": 0,
          "max_available_loan_amount": 2000.00,
          "max_loan_amount": 0,
          "max_monthly_payment": 0.00,
          "product_code": 2
        },
        {
          "available_loan_terms": [
            3,
            6,
            4,
            5
          ],
          "default_amount": 0,
          "default_term": 0,
          "document_required": 0,
          "max_available_loan_amount": 4000.00,
          "max_loan_amount": 0,
          "max_monthly_payment": 1300.00,
          "product_code": 101
        },
        {
          "available_loan_terms": [
            3,
            6,
            4,
            5
          ],
          "default_amount": 0,
          "default_term": 0,
          "document_required": 0,
          "max_available_loan_amount": 3000.00,
          "max_loan_amount": 0,
          "max_monthly_payment": 1510.00,
          "product_code": 100
        }
      ],
      "scoring_through_zoral": "0"
    }
  ],
  "description": "...",
  "requestId": "...",
  "status": 8888
}

I would like to get values of fields 'default_term' and 'max_available_loan_amount' only for product with code 2. I use restassured I've tried

 List<List> lists = response.body().jsonPath().getList("data.products");

but this code returns list with only one item, I mean all products in one item... how can I get information for first item in products array from this json ???



Solution 1:[1]

Step 1: Create POJO to mapping

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Product {
    @JsonProperty("default_term")
    private int default_term;
    @JsonProperty("max_available_loan_amount")
    private double max_available_loan_amount;
}

Step 2: Extract response and deserialize to object.

Product product = ...jsonPath().getObject("data[0].products.find {it.product_code == 2}", Product.class);
System.out.println(product);
//Product(default_term=15, max_available_loan_amount=2000.0)

Solution 2:[2]

This could work

List<Integer> li_defaultTerm = response.getBody().jsonPath().get("$..products[?(@.product_code=='2')].default_term");
System.out.println(li_defaultTerm.get(0));
//15

List<Double> li_MaxAvailLoanAmount = response.getBody().jsonPath().get("$..products[?(@.product_code=='2')].max_available_loan_amount");
System.out.println(li_MaxAvailLoanAmount.get(0));
//2000.0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lucas-nguyen-17
Solution 2 vijay atm