Assert portion of JSON with nested objects and arrays

Hello

I have an application that uses an API to retrieve data to display to the user in a chart.

I would like to use Postman to ensure the data being returned is consistent over time ( eg value A for July 21, 11:05 pm is the same today as it was originally) as we have had database issues previously.

The data is included in a number of nested objects and comprise just one portion of the API return. I don’t want to compare the entire JSON as there is continuing development on this project that will likely affect some of these other objects and arrays.

I thought there might be some type of JSON.includes function but as of yet cannot find. See screenshot with a portion of the sample JSON with the portion I want to include in the assertion highlighted in yellow.

Hi @kirbywanner,

Welcome to the Postman community! Using the pm library you would be able to check the data and group field in the response against the expected data with the following test script assuming you want to get only the first record from series_list:

  1. pm.test(“check data is expected”, function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.graph_options.series_list[0].data).to.eql(data);
    });
  2. pm.test(“check group is expected”, function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.graph_options.series_list[0].group).to.eql(group);
    });

Since series_list is an array, you will not be able to access just the values in data and group for all the records, you would have to test the series_list array itself which would be done as given below:
3. pm.test(“check series list is expected”, function () {
var jsonData = pm.response.json();
pm.expect(jsonData.graph_options.series_list).to.eql(series_list);
});

Halo !
Thanks for sharing this info… I have difficulty to make tests in Postman to validate the response body has the value I want. For example I want to check :slight_smile:
response id should be a number
response name should be a string, etc

I have tried your script but it doesn’t work. Here is my structure data response. Please take a look

{
“data”: [
{
“id”: 114,
“name”: “tes tanggal repayment”,
“category”: null,
“image_url”: null,
“tenor”: 18,
“margin”: 30,
“margin_info”: “15% per tahun”,
“margin_info_period”: “6 bulan”,
“unit_price”: 15000000,
“unit_sold”: 1,
“unit_available”: 0,
“lenders_count”: 1,
“fundraising_info”: “1 unit telah dibiayai oleh 1 orang, tersisa 0 unit lagi”,
“fundraising_percentage”: 100,
“borrower”: {
“id”: 118687,
“name”: “PT. Arabika Nusantara”,
“description”: “”,
“image_url”: null,
“total_financing”: 43,
“member”: 50,
“location”: “Agroyasa Farm”,
“latitude”: “-6.7992111”,
“longitude”: “106.7002202”
}
});