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

时间:2019-04-01 21:09:09

标签: javascript json lodash postman

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

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

在下面的示例中,我想获取响应中所有“ IsArchived”属性的计数

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

我该怎么办?预先感谢

{
    "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
}

1 个答案:

答案 0 :(得分:0)

这是一个相当具体的解决方案,但希望对您有所帮助。该说明已添加为注释:

// 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无效:

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