javascript - postman :从 JSON 数组中获取数组值并将它们设置为 postman 变量

标签 javascript postman

我也有同样的问题

postman get values from array and set it,dynamically ,on env variables

但我找不到如何为自己修改最佳答案的方法。

JSON:

{
"meta": {
    "code": 200,
    "requestId": "5c871f361ed2196e43a00982"
},
"response": {
    "venues": [
        {
            "id": "536e7cac498ea9201478f0bb",
            "name": "Yeni Camii Alımyeri 1",
            "location": {
                "lat": 41.06841467513242,
                "lng": 40.69481069825992,
                "labeledLatLngs": [
                    {
                        "label": "display",
                        "lat": 41.06841467513242,
                        "lng": 40.69481069825992
                    }
                ],
                "distance": 60953,
                "cc": "TR",
                "country": "Türkiye",
                "formattedAddress": [
                    "Türkiye"
                ]
            },
            "categories": [
                {
                    "id": "530e33ccbcbc57f1066bbff3",
                    "name": "Town",
                    "pluralName": "Towns",
                    "shortName": "Town",
                    "icon": {
                        "prefix": "https://ss3.4sqi.net/img/categories_v2/parks_outdoors/neighborhood_",
                        "suffix": ".png"
                    },
                    "primary": true
                }
            ],
            "referralId": "v-1552359222",
            "hasPerk": false
        },
        {
            "id": "5a22b75566611634a15d1f68",
            "name": "Osmançavuş Mah.",
            "location": {
                "lat": 40.714043,
                "lng": 41.209717,
                "labeledLatLngs": [
                    {
                        "label": "display",
                        "lat": 40.714043,
                        "lng": 41.209717
                    }
                ],
                "distance": 64990,
                "cc": "TR",
                "city": "Erzurum",
                "country": "Türkiye",
                "formattedAddress": [
                    "Erzurum",
                    "Türkiye"
                ]
            },
            "categories": [
                {
                    "id": "530e33ccbcbc57f1066bbff3",
                    "name": "Town",
                    "pluralName": "Towns",
                    "shortName": "Town",
                    "icon": {
                        "prefix": "https://ss3.4sqi.net/img/categories_v2/parks_outdoors/neighborhood_",
                        "suffix": ".png"
                    },
                    "primary": true
                }
            ],
            "referralId": "v-1552359222",
            "hasPerk": false
        },
        {
            "id": "55e0acd1498e06a1c0bd34ca",
            "name": "Ören Beldesi",
            "location": {
                "lat": 41.17862276665214,
                "lng": 40.886770827531386,
                "labeledLatLngs": [
                    {
                        "label": "display",
                        "lat": 41.17862276665214,
                        "lng": 40.886770827531386
                    }
                ],
                "distance": 78451,
                "cc": "TR",
                "country": "Türkiye",
                "formattedAddress": [
                    "Türkiye"
                ]
            },
            "categories": [
                {
                    "id": "530e33ccbcbc57f1066bbff3",
                    "name": "Town",
                    "pluralName": "Towns",
                    "shortName": "Town",
                    "icon": {
                        "prefix": "https://ss3.4sqi.net/img/categories_v2/parks_outdoors/neighborhood_",
                        "suffix": ".png"
                    },
                    "primary": true
                }
            ],
            "referralId": "v-1552359222",
            "hasPerk": false
        },
        {
            "id": "53e33833498ec6568c338076",
            "name": "Mapsino Köy Çıkışı",
            "location": {
                "lat": 40.81617736816406,
                "lng": 40.26142501831055,
                "labeledLatLngs": [
                    {
                        "label": "display",
                        "lat": 40.81617736816406,
                        "lng": 40.26142501831055
                    }
                ],
                "distance": 34825,
                "cc": "TR",
                "country": "Türkiye",
                "formattedAddress": [
                    "Türkiye"
                ]
            },
            "categories": [
                {
                    "id": "530e33ccbcbc57f1066bbff3",
                    "name": "Town",
                    "pluralName": "Towns",
                    "shortName": "Town",
                    "icon": {
                        "prefix": "https://ss3.4sqi.net/img/categories_v2/parks_outdoors/neighborhood_",
                        "suffix": ".png"
                    },
                    "primary": true
                }
            ],
            "referralId": "v-1552359222",
            "hasPerk": false
        }
    ],
    "confident": false
}

我试图获取 response.venues.id 的值,有时我最终不得不手动创建 29 个变量。

如果有人可以告诉我如何编辑此代码以处理我的 JSON 响应,那就太好了。

let idCount = 1
let nameCount = 1
let typeCount = 1
_.each(_.first(pm.response.json()).variables, (arrItem) => {
pm.environment.set(`varID${idCount ++}`,  arrItem.id);
pm.environment.set(`varName${nameCount ++}`, arrItem.name);
pm.environment.set(`varType${typeCount ++}`, arrItem.type);

})

最佳答案

您可以用以下内容替换现有脚本:

let responseBody = pm.response.json(),
  venues = _.get(responseBody, 'response.venues');

_.forEach(venues, (venue, index) => {
    pm.environment.set(`varID${++index}`,  venue.id);
});

这会将您所有的 ID 作为变量存储在环境中,它会像这样显示: Environment variables

解释:

首先,我们从您的响应主体中获取 venues 以便轻松地在其上运行循环。 然后我们只需对数组中的项目运行一个简单的 forEach 循环,并将每个数组项目的相应 ID 存储在环境变量中。

我们正在使用 lodash library对于像 _.get 和 _.forEach 这样内置于 postman 中的函数。

关于javascript - postman :从 JSON 数组中获取数组值并将它们设置为 postman 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55113671/

相关文章:

javascript - 为什么带括号的 string.match(/(regexp)/) 返回匹配两次?

node.js - 如何在 cookie 中存储 jwt 并在重定向页面时将其传递给身份验证函数?

Symfony 4 和 Postman 从后期数据 POST 请求访问文件

c# - 按顺序调用多个 API 来检索和传递 token C#

javascript - 从每个 2 个标记 javascript 显示更多方向

javascript - 将 Mat Dialog Angular 4 中的多个数据传递回父级

java - 如何在 Springboot 应用程序中捕获通过 POSTMAN 发送的 JSONObject?

amazon-web-services - Postman 请求 Amazon Alexa Smart Home Skill LWA 时客户端身份验证失败

javascript - React 组件在浏览器中正确呈现,但呈现 : "Only a ReactOwner can have refs" 时 Jest 测试错误

javascript - Node.js fs.open() 在尝试打开超过 4 个命名管道 (FIFO) 后挂起