Postman - 如何计算 JSON 响应中特定对象的出现次数

2022-01-22 00:00:00 json postman javascript

我是 JSON 和 Postman 的新手.我相信我正在尝试做一些非常简单的事情.

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

我创建了一个 GET 请求,它将获得如下所示的 JSON 响应.

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

在下面的示例中,我想获取响应中所有IsArchived"属性的 count;

In the example below I want to get the count of All "IsArchived" attributes in the response;

这些属性的数量因响应而异.

The number of those attributes will vary from response to response.

我该怎么做?提前致谢

{
    "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",
                    "IsArchived": false
                },
                {
                    "Id": 2,
                    "DisplayName": "Short Description",
                    "IsArchived": false
                },
                {
                    "Id": 33,
                    "DisplayName": "Long Description",
                    "IsArchived": false
                },
            ]
        },
        {
            "Id": "5ed8746b-0fa8-4022-8216-ad3af17db91f",
            "Name": "Somethingelse",
            "FieldDefinitions": [
                {
                    "Id": 123,
                     "DisplayName": "Attribution",
                    "IsArchived": false
                },
                {
                    "Id": 1584,
                    "DisplayName": "FC1",
                    "IsArchived": false
                },
                {
                    "Id": 623,
                    "DisplayName": "Sizes",
                    "IsArchived": false,
                    "Owner": {
                        "Id": 208,
                        "Name": "The Boss"
                    },
                    "Unit": "",
                    "Options": [
                        {
                            "Id": 1,
                            "Value": "XS"
                        },
                        {
                            "Id": 2,
                            "Value": "S"
                        },
                        {
                            "Id": 3,
                            "Value": "M"
                        }
                    ]
                }
             ]
        }
    ],
    "IsArchived": false
    "Version": 1
}

推荐答案

这是一个相当具体的解决方案,但我希望它有所帮助.描述被添加为评论:

It is a rather specific solution but I hope it helps. The description is added as comments:

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

// Create a count variable which will be increased by 1 everytime IsArchived occurs
var count = 0;

function countIsArchived() {
    // Loop through the FieldGroupsArray
    _.each(jsonData.FieldGroups, (fieldGroupsArray) => {
        // Loop through the FieldDefinitionsArray
        _.each(fieldGroupsArray.FieldDefinitions, (fieldDefinitionsArray) => {
            // Check if IsArchived exists
            if(fieldDefinitionsArray.IsArchived) {
                // Increase count by 1
                count++;
            }
        });
    });

    // IF you want it:
    // Check if IsArchived exists on the top level of the JSON response and increase count
    if(jsonData.IsArchived) {
        count++;
    }
    // IF you want it:
    // Create a Postman environment variable and assign the value of count to it
    pm.environment.set("count", count);
}

附加信息:

不需要后面的,.它使 JSON 无效:

The , after the following object is not needed. It invalidates the JSON:

{
    "Id": 33,
    "DisplayName": "Long Description",
    "IsArchived": false
},  <--

相关文章