0

I'm completely new to postman. I'm able to GET the following results below.

How would I go about defining my output from the array. So for example, the call output should only list the Real_name,Title and emailaddress fields?



{
    "ok": true,
    "offset": "DDGGDFRDFG",
    "members": [
        {
            "id": "100",
            "real_name": "First Surname",
            "tz": "Europe/London",
            "profile": {
                "title": "Sales",
                ],
                "email": "First.Surname@test.com",
            },
            "is_admin": False,
                ]
            }
        },
        {
            "id": "101",
            "real_name": "First1 Surname1",
            "tz": "Europe/London",
            "profile": {
                "title": "Acccounts",
                ],
                "email": "First1.Surname1@test.com",
            },
            "is_admin": False,
                ]
            }
        },
Lightman
  • 1
  • 1

1 Answers1

0

You can add a post processing java-script in Tests of Postman.

It can filter or map operation of JSON array.

Your JSON out is not validate, I changed a bits

{
    "ok": true,
    "offset": "DDGGDFRDFG",
    "members": [
        {
            "id": "100",
            "real_name": "First Surname",
            "tz": "Europe/London",
            "profile": {
                "title": "Sales",
                "email": "First.Surname@test.com"
            },
            "is_admin": false
        },
        {
            "id": "101",
            "real_name": "First1 Surname1",
            "tz": "Europe/London",
            "profile": {
                "title": "Acccounts",
                "email": "First1.Surname1@test.com"
            },
            "is_admin": false
        }
    ]
}

I think you want to shows only this JSON

[
  {
    "real_name": "First Surname",
    "title": "Sales",
    "email": "First.Surname@test.com"
  },
  {
    "real_name": "First1 Surname1",
    "title": "Acccounts",
    "email": "First1.Surname1@test.com"
  }
]

This Javascript will makes result. You needs to put in Tests tab section.

var jsonData = JSON.parse(responseBody);
var real_names = jsonData.members
    .map (el => {
        return {
            real_name : el.real_name,
            title : el.profile.title,
            email : el.profile.email
        }
    });
console.log(JSON.stringify(real_names, null, 4))

Test by Postman,

enter image description here

line 1 : var jsonData = JSON.parse(responseBody); API response to makes JSON format and save into jsonData variable

line 2~9 : jsonData.members is array data type due to start [ to end ] Then using map function with just pick-up real_name, title and email.

line 10: return object array from map, convert JSON format and indent with 4 spaces. console.log() will print to Console of postman.

enter image description here

Bench Vue
  • 5,257
  • 2
  • 10
  • 14