1

I have this JSON Response from API call

[
    {
        "id": 20599,
        "name": "Deliver",
        "options": [
            {
                "id": 63775,
                "name": "Item",
                "dataType": "SelectMultiOption",
                "required": false,
                "options": [
                    {
                        "id": 426,
                        "name": "Towels"
                    },
                    {
                        "id": 427,
                        "name": "Toothbrush"
                    },
                    {
                        "id": 428,
                        "name": "Pillow"
                    }
                ]
            }
        ]
    }
]

I am using this code to get the id of the service "Deliver"

var data = JSON.parse(responseBody); 
var loop_count = 0

for (count = 0; count < data.length; count++)
{
    if (data[count].name == "Deliver")
    {
        var job_id = data[count].id;
        postman.setEnvironmentVariable("service_id", job_id);

    }
}

The questions are:

  1. How can I get value from array "options", I need to get the "id": 63775 and store as "item_id" and the "name":"Item" as "item_name" postman variables.
  2. Then I need to select the "options" nested in record "Item" and select the option "name": "Toothbrush" and store in postman variable "svc_optn_optn_name" and it's "id" stored in "svc_optn_optn_id"
1
  • The outer options array will only contain 1 object or there may be multiple also, and what is postman variable?
    – hygull
    Commented Dec 14, 2018 at 16:41

3 Answers 3

1

Here I am giving my own suggestion for your problem with few lines of code. I am not sure, how are you going to use these values. I also don't know if the outer options array will always have 1 item or more. I have just tried to satisfy your questions.

Please ask/comment, if you have more doubts or I am wrong.

I have created a function getAllPostmanDataFrom(obj) which takes object as parameter which is the value of data[count], gathers necessary info in other object postmanObj and returns it to the caller.

function getAllPostmanDataFrom(obj) {
    const item_id = obj.options[0].id;
    const item_name = obj.options[0].name;
    const svc_optn_optn_name = obj.options[0].options[1].name;
    const svc_optn_optn_id = obj.options[0].options[1].id;

    const postmanObj = {item_id, item_name, svc_optn_optn_id, svc_optn_optn_name}; // Return object
    return postmanObj;
}

var data = [
    {
        "id": 20599,
        "name": "Deliver",
        "options": [
            {
                "id": 63775,
                "name": "Item",
                "dataType": "SelectMultiOption",
                "required": false,
                "options": [
                    {
                        "id": 426,
                        "name": "Towels"
                    },
                    {
                        "id": 427,
                        "name": "Toothbrush"
                    },
                    {
                        "id": 428,
                        "name": "Pillow"
                    }
                 ]
            }
        ]
    }
]

var count = 0;
var obj = data[count];
var postmanObj = getAllPostmanDataFrom(obj);
//var {item_id, item_name, svc_optn_optn_id} = postmanObj;

console. log(postmanObj) 
/*
console.log(item_id);
console.log(item_name);
console.log(svc_optn_optn_id);
console.log(svc_optn_optn_name);
*/

Finally, you can use values contained in postmanObj as follows:.

postman.setEnvironmentVariable("item_id", postmanObj.item_id);
postman.setEnvironmentVariable("item_name",  postmanObj.item_name);

And so on.

1

This is the solution

var data = JSON.parse(responseBody); 
variable named as data
var loop_count = 0

for (count = 0; count < data.length; count++)
{
    if (data[count].name == "Deliver")
    {
        var job_id = data[count].id;
        postman.setEnvironmentVariable("service_id", job_id);
        var job1_name = data[count].options[0].name;
        postman.setEnvironmentVariable("item_name", job1_name);
        var job2_id = data[count].options[0].id;
        postman.setEnvironmentVariable("item_id", job2_id);
        var job3_id = data[count].options[0].options[1].id;
        postman.setEnvironmentVariable("svc_optn_optn_id", job3_id);
        var job4_name = data[count].options[0].options[1].name;
        postman.setEnvironmentVariable("svc_optn_optn_name", job4_name);

    }
0
const data = JSON.parse(responseBody);
data.forEach(item => {
    console.log(item.id); // deliver object id.
    item.options.forEach(option => {
        console.log(`Option Id ${option.id}`); // option id
        postman.setEnvironmentVariable("service_id", option.id);
        option.options(optionItem => {
            if(optionItem.name == 'Toothbrush'){
                postman.setEnvironmentVariable("svc_optn_optn_name", optionItem.name);
                postman.setEnvironmentVariable("svc_optn_optn_id", optionItem.id);
            }
        });
    });
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.