Postman - 如何计算 JSON 响应中特定对象的出现次数 [英] Postman - How to count occurrences of a specific object in a JSON response

查看:20
本文介绍了Postman - 如何计算 JSON 响应中特定对象的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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
},  <--

这篇关于Postman - 如何计算 JSON 响应中特定对象的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆