1

I am new to JSON and Postman. I think I'm trying to do something very simple.

I have created a POST request which will get a JSON response like the one below.

In the example below from inside of the FieldGroups array I want to get the value of Id but only if the value of Name is General

How can I do it? Thanks in advance.

{
    "Id": 1328,
    "Name": "AAA Test",
    "Owner": {
        "Id": 208,
        "Name": "The Boss"
    },
    "FieldGroups": [
        {
            "Id": "c81376f0-6ac3-4028-8d61-76a0f815dbf8",
            "Name": "General",
            "FieldDefinitions": [
                {
                    "Id": 1,
                    "DisplayName": "Product Name"
                },
                {
                    "Id": 2,
                    "DisplayName": "Short Description"
                },
                {
                    "Id": 33,
                    "DisplayName": "Long Description"
                },
            ]
        },
        {
            "Id": "5ed8746b-0fa8-4022-8216-ad3af17db91f",
            "Name": "Somethingelse",
            "FieldDefinitions": [
                {
                    "Id": 123,
                     "DisplayName": "Attribution"
                },
                {
                    "Id": 1584,
                    "DisplayName": "FC1"
                },
                {
                    "Id": 623,
                    "DisplayName": "Sizes",
                    "Owner": {
                        "Id": 208,
                        "Name": "The Boss"
                    },
                    "Unit": "",
                    "Options": [
                        {
                            "Id": 1,
                            "Value": "XS"
                        },
                        {
                            "Id": 2,
                            "Value": "S"
                        },
                        {
                            "Id": 3,
                            "Value": "M"
                        },
                    ]
                }
             ]
        }
    ],
    },
    "Version": 1
}

1 Answer 1

1

Postman has built-in support of a JavaScript utility library called Lodash (the _ in the code) and you can use its features to loop through the JSON array in a more consice way.

Since I am not exactly sure what you want to achieve, just try to adapt below code to your problem.

// Convert the response body to a JSON object
var jsonData = pm.response.json()

// Create a variable to which you will assign the value of Id later on
var generalId;

function setGeneralID() {
    _.each(jsonData.FieldGroups, (arrayItem) => {
        if(arrayItem.Name  === 'General') {
            // Assign the value of Id to the generalId variable
            generalId = arrayItem.Id;

            // OR (depending on what you want to do)
            // Create a Postman environment variable and assign the value of Id to it
            pm.environment.set("Id", arrayItem.Id);
        }
    });
}

I hope I could help.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot 0xFEFFEFE ! That is exactly what I was looking for!
Cool, glad I could help. :)

Your Answer

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.