繁体   English   中英

邮递员从 JSON 中获取值,其中使用 javascript 等于数组中的值

[英]Postman get value from JSON where equals a value in array using javascript

目前使用的Postman最新版本:6.7.4(最新)

我正在尝试从 JSON 响应正文中获取一个值并将其存储在环境变量中,但值 'username' 应该等于我的首选用户名。

通常我会提取这样的值:

var jsonData = pm.response.json();
pm.environment.set("useridToken", jsonData.Customers[0].userid);

这会给我列表中的第一项,但我希望从列表中获得第一项或第二项。 我希望得到的userid ,其中username等于“比利”的例子。

身体反应的输出:

{
"Customers": [
    {
        "id": 24,
        "userid": 73063,
        "username": "BOB",
        "firstname": "BOB",
        "lastname": "LASTNAME
    },
    {
        "id": 25,
        "userid": 73139,
        "username": "Billy",
        "firstname": "Billy",
        "lastname": "lasty"
    }
   ]
}

有小费吗?

我记得在 SoapUI 中是这样的:

$.channels[?(@.is_archived=='false')].id[0]

我想在 Postman 的 JS 中不可能做到这一点?

您可以使用: Array.prototype.find()

 const data = { "Customers": [{ "id": 24, "userid": 73063, "username": "BOB", "firstname": "BOB", "lastname": "LASTNAME" }, { "id": 25, "userid": 73139, "username": "Billy", "firstname": "Billy", "lastname": "lasty" } ] }; const user = data.Customers.find(u => u.username === 'Billy'); const userid = user ? user.userid : 'not found'; console.log(user); console.log(userid);

find()作为另一个答案指出是这里的最佳解决方案,但如果用户名不是唯一的,并且您想要一组用户名是“Billy”的用户,则使用filter()

 const jsonData = { "Customers": [{ "id": 24, "userid": 73063, "username": "BOB", "firstname": "BOB", "lastname": "LASTNAME" }, { "id": 25, "userid": 73139, "username": "Billy", "firstname": "Billy", "lastname": "lasty" } ] } console.log(jsonData.Customers.filter(c => c.username === 'Billy'))

在 Postnam 测试脚本中,您可以使用some Javascript 功能。 就你而言,方法太多了。 我将向您展示如何使用Array.find函数解决您的问题:

var jsonData = pm.response.json();
var user = jsonData.Customers.find(function(user) {
    return user.username === 'Billy';
    // OR you could config username in postman env
    // return user.username === pm.variables.get("username_to_find"); 
});
pm.environment.set("useridToken", user.userid);

您的userid也可以使用filter ,如下所示 -

 const data = { "Customers": [{ "id": 24, "userid": 73063, "username": "BOB", "firstname": "BOB", "lastname": "LASTNAME" }, { "id": 25, "userid": 73139, "username": "Billy", "firstname": "Billy", "lastname": "lasty" } ] }; const username = 'Billy'; const user = data.Customers.filter(obj => obj.username.toLowerCase() === username.toLowerCase())[0]; const userid = user ? user['userid'] : null; console.log(userid);

注意: .toLowerCase()在这里是可选的,您可以根据自己的情况使用它。

然后你可以简单地将它设置为 -

pm.environment.set("useridToken", userid);

您还可以使用Lodash迭代数据并设置该值:

_.each(pm.response.json().Customers, (item) => {
    if (item.username === "Billy") {
        pm.globals.set('username', item.username)
    }
})

尝试这个

const userid = data.Customers.find(u => u.username === 'Billy') || 'not found';

重复当前投票最高的答案,稍作修改。
还添加了一个受其他 Lodash 答案启发的解决方案。 1

 const jsonData = { Customers: [{ id: 24, userid: 73063, username: 'BOB', firstname: 'BOB', lastname: 'LASTNAME' }, { id: 25, userid: 73139, username: 'Billy', firstname: 'Billy', lastname: 'lasty' }]}; for (const i in jsonData.Customers) { console.log('userid of customer['+i+']: '+jsonData.Customers[i].userid); } const userId_UsingFind = (name) => { const user = jsonData.Customers.find(item => item.username === name); return user ? user.userid : user; }; console.log('Using .find(), userid of "Billy": '+userId_UsingFind('Billy')); console.log('Using .find(), userid of "Joe": '+userId_UsingFind('Joe')); const userId_Native = (name) => { for (const i in jsonData.Customers) { if (jsonData.Customers[i].username === name) { return jsonData.Customers[i].userid; } } }; console.log('Native loop, userid of "Billy": '+userId_Native('Billy')); console.log('Native loop, userid of "Joe": '+userId_Native('Joe'));

如代码所示,使用.find()的解决方案既简短又优雅。


1假设所需的结果是第一个Billyuserid 要为所有出现的Billy检索userid :s数组,请参阅返回 userid:s 数组答案

这个答案展示了一个使用 JavaScript 库Lodash的解决方案。
并不意味着作为推荐,而只是为了证明可以使用 Lodash。
它的灵感来自另一个 lodash answer 1

 const jsonData = { Customers: [{ id: 24, userid: 73063, username: 'BOB', firstname: 'BOB', lastname: 'LASTNAME' }, { id: 25, userid: 73139, username: 'Billy', firstname: 'Billy', lastname: 'lasty' }] }; const userId_Lodash = (name) => { let userId; _.forEach(jsonData.Customers, (item) => { if (item.username === name) { userId = item.userid; } }); return userId; }; console.log('Lodash loop, userid of "Billy": ' + userId_Lodash('Billy')); console.log('Lodash loop, userid of "Dave": ' + userId_Lodash('Dave'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.19/lodash.js"></script>

对于手头的问题,我没有看到使用 Lodash 库的任何特殊原因。
但在其他例子中,它可能更有意义。


1发布的问题没有说明期望的结果是第一个匹配Billy userid ,还是所有这样的用户 ID:s。 这个答案给出了第一个命中。

这个答案的灵感来自另一个输出 array 的答案 1

原始发布者没有明确说明所需的输出应该是单个userid (大概是第一次出现?) - 还是包含所有userid的数组:s 匹配“比利”。

此答案显示了使用Lodash解决后一种情况的方法。

 const jsonData = { Customers: [{ userid: 73063, username: 'BOB' }, { userid: 73138, username: 'Billy' }, { userid: 74139, username: 'Billy' }] }; const userIds = []; _.forEach(_.filter(jsonData.Customers, c => c.username === 'Billy'), item => { userIds.push(item.userid); }); console.log(userIds);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.19/lodash.js"></script>

1该答案非常有用,因为它暗示了如何过滤掉Customers数组的相关对象。 但是,原始发布者想要(一个数组) userid (s),它是一个number ,而不是包含userid :s 的对象数组。 这就是我在这里的答案的不同之处。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM