2

I am trying to set a variable from following phone number with value: "+33652556777" (index 4 in JSON attached below) which is the last object in contacts (index 4).

To do so is pretty simple:

let jsonData = pm.response.json();
console.log (jsonData.contacts[4].phone_numbers[0].value) 
const PhoneNumber = jsonData.contacts[4].phone_numbers[0].value
pm.environment.set("Jacky", PhoneNumber);

Since I have to use different query parameters to filter by eg. created_at=asc, desc, the property of the phone_numbers order might change index number and I won’t be able to fetch desire phone number "+33652556777" instead it will set a different phone number which I cannot allow.

I know there is way to fetch our number and make it variable for next requests, which is iterating over properties or keys in the object " for....in or for...of " but for some reason I cannot achieve it.

What I could achieve is to get through first object "contacts" but impossible to get to its nested array "phone_numbers". Here is how I did it:

let jsonData = pm.response.json();
let contact;
for (let filter of jsonData.contacts){
if (filter.last_name == "Rowland"){
 contact = filter;
}} 
console.log (contact);

Could you please help?

Here goes the JSON body response:

{
 "contacts": [
 {
 "id": 11121211,
 "direct_link": "https://example.example",
 "first_name": "test1",
 "last_name": "test",
 "company_name": "test",
 "information": null,
 "is_shared": true,
 "created_at": 1582798926,
 "updated_at": 1582798926,
 "emails": [],
 "phone_numbers": [
 {
 "id": 60065270,
 "label": "Work",
 "value": "+33134567666"
 }
 ]
 },
 {
 "id": 22222222,
 "direct_link": "https://example.example",
 "first_name": null,
 "last_name": null,
 "company_name": null,
 "information": null,
 "is_shared": true,
 "created_at": 1583686067,
 "updated_at": 1583686067,
 "emails": [],
 "phone_numbers": [
 {
 "id": 22266444,
 "label": "Work",
 "value": "+33134567899"
 }
 ]
 },
 {
 "id": 33333564,
 "direct_link": "https://example.example",
 "first_name": "Jessica",
 "last_name": "Biel",
 "company_name": "N-Sync",
 "information": null,
 "is_shared": true,
 "created_at": 1583686086,
 "updated_at": 1583686086,
 "emails": [],
 "phone_numbers": []
 },
 {
 "id": 45678901,
 "direct_link": "https://example.example",
 "first_name": null,
 "last_name": null,
 "company_name": null,
 "information": null,
 "is_shared": true,
 "created_at": 1583686105,
 "updated_at": 1583686105,
 "emails": [],
 "phone_numbers": [
 {
 "id": 22266444,
 "label": "Work",
 "value": "+33134567333"
 }
 ]
 },
 {
 "id": 56789123,
 "direct_link": "https://example.example",
 "first_name": "Jacky",
 "last_name": "Rowland",
 "company_name": "Test Company1",
 "information": "",
 "is_shared": true,
 "created_at": 1583745888,
 "updated_at": 1608556499,
 "emails": [
 {
 "id": 76594398,
 "label": "Work",
 "value": "[email protected]"
 }
 ],
 "phone_numbers": [
 {
 "id": 60650277,
 "label": "Mobile",
 "value": "+33652556777"
 }
 ]
 }
 ],
 "meta": {
 "count": 6,
 "total": 241,
 "current_page": 1,
 "per_page": 5,
 "next_page_link": "https://example.example",
 "previous_page_link": null
 }
}
asked Feb 17, 2021 at 13:03

2 Answers 2

2

You could use something basic like this:

_.each(pm.response.json().contacts, (contact) => {
 if(contact.last_name === "Rowland") {
 pm.environment.set(`${contact.first_name}_${contact.last_name}_number`, contact.phone_numbers[0].value)
 }
})

There are probably better and more performant ways to do this but if you just want to set a variable for that contact, no matter where they are in the response - This would work :D

answered Feb 17, 2021 at 13:28

Comments

0

you can use forEach or _.each as danny mentioned to get all numbers else use:

console.info(jsonData.contacts.find((a)=>a.first_name==="Jacky").phone_numbers[0].value)

use array.find to find the contact with first_name jacky adn then get phone_number[0].value from it.

if you want all numbers from that array then use:

console.info(jsonData.contacts.find((a)=>a.first_name==="Jacky").phone_numbers.map((a)=>a.value))

here we map the result to get only the numbers from phone_number array.

is it what you looking for !?

answered Feb 17, 2021 at 16:04

2 Comments

Multiple different ways to do the same thing. Might need to modify your answer to push that into a variable, as the was the original reason, rather than writing it to the console.
No worries, after checking the log in the console it all made sense and I was able to set my variables. Thanx again

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.