Postman:从JSON Array获取数组值并将它们设置为postman变量

问题描述 投票:1回答:1

我有同样的问题

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);

})

javascript postman
1个回答
3
投票

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

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 and _.forEach这样内置于邮递员的功能。

© www.soinside.com 2019 - 2024. All rights reserved.