Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get JSONobject from JSONArray in postman

Tags:

postman

I am trying to automize a registration scenario in postman using test scripts I have the following JsonArray as a response:

[
    {
        "id": 1,
        "name": "user_A",
        "cntkp": "martin",
        "company": "kreativ",
        "tel": "12345678",
        "email": "[email protected]"
        "street": "str. 0001",
        "city": "DEF",
    }
    ......
    ......
    ......
    {
        "id": 4,
        "name": "user_B",
        "cntkp": "martin",
        "company": "kreativ",
        "tel": "12345678",
        "email": "[email protected]"
        "street": "str. 0002",
        "city": "NJ",
    }
    ......
    ......
    ......
    {
        "id": 10,
        "name": "User_C",
        "cntkp": "martin",
        "company": "kreativ",
        "tel": "12345678",
        "email": "[email protected]"
        "street": "str. 0003",
        "city": "ABC",
    }
    ......
]

the array length can be dynamic and changed (in this sample is 10) and want to find the object with special email (somewhere in the array) and then get the ID from that object and make the assertion based on JsonData from this object (catch elements e.g. check name).

how can I do that?

thanks for the support.

I send a GETrequest to get all Data from Registration DB. as response I get a JsonArray from Json Array I need the specific Object for the assertion (e.g. object with email user_B in sample) . I know my Email address and base on it I have to findout the ID from Object . I can do it when I know which ID is my ID but in case it is dynamic I don't know how to search an array for it in postman to get ID

For example, to assert the company name

pm.expect(jsonData[0].company).to.equal(pm.environment.get("regDB_new_company"))

but if I dont know the ID ( only know my email) I have first to find out the ID of Object then I can asser it.

e.g. in this case first, find the object with email "[email protected]" then from that object get ID element (in this case 4) then I want to assert for all data from the object

like image 826
AKADO Avatar asked Feb 13 '26 09:02

AKADO


1 Answers

Thanks Danny, I found the solution

var arr = pm.response.json()

for(i = 0; i < arr.length; i++) {
    if (arr[i].email == "[email protected]") {
        pm.environment.set("personID", arr[i].id)
    }
}
like image 157
AKADO Avatar answered Feb 17 '26 19:02

AKADO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!