简体   繁体   English

如何使用测试从 Postman 中的 JSON 响应中提取特定值?

[英]How do I extract specific values from the JSON response in Postman using a test?

I need to be able to parse through the JSON results from a Postman response to just pull out the name value in the console output.我需要能够解析来自 Postman 响应的 JSON 结果,以便在控制台 output 中提取名称值。 My JSON looks like this:我的 JSON 看起来像这样:

"total_count": 7,
"entries": [
    {
        "type": "collaboration",
        "id": "21829111111",
        "created_by": null,
        "created_at": "2020-03-24T05:37:09-07:00",
        "modified_at": "2020-03-24T05:37:09-07:00",
        "expires_at": null,
        "status": "accepted",
        "accessible_by": {
            "type": "group",
            "id": "3085402",
            "name": "Test",
            "group_type": "managed_group"

Most of the examples I find are using extracted value to create a variable but I really just need a simple list I can copy and paste.我发现的大多数示例都使用提取的值来创建变量,但我真的只需要一个可以复制和粘贴的简单列表。

I've used this code to do something similar but I am not sure how to modify it for us with this data:我用这段代码做了类似的事情,但我不知道如何用这些数据为我们修改它:

var response = JSON.parse(responseBody);
var usernames = response.map(function(account){
    return account.credentials.userName;
});
console.log(usernames);

Any help is appreciated.任何帮助表示赞赏。

Let's walk through it:让我们来看看它:

var response = JSON.parse(responseBody);

JSON.parse() is taking the JSON response in as a string, and converting it to a JS object, which will then be stored in response . JSON.parse()将 JSON 响应作为字符串,并将其转换为 JS object,然后将其存储在response中。

response.map(...)

The map() function is called on an array and takes a function as a parameter. map() function 在数组上调用,并以 function 作为参数。 It calls the function you provide it once for each element in the array that it's called on.它调用 function 您为调用它的数组中的每个元素提供一次。

For your particular case, we'll need to modify this a little, since response is an object, rather than an array, and the value you're after, name , is nested in the entries array inside of response .对于您的特定情况,我们需要稍微修改一下,因为response是 object,而不是数组,并且您所追求的值name嵌套在response内部的entries数组中。 To account for this, we're going to call map() directly on the nested array, and set it equal to a new variable to store the result, like this:考虑到这一点,我们将直接在嵌套数组上调用map() ,并将其设置为等于一个新变量来存储结果,如下所示:

var names = response.entries.map(...);

Now, we can pass map() a function that will extract the name from each element in the entries array.现在,我们可以传递map()一个 function ,它将从条目数组中的每个元素中提取名称。 Our function needs to accept the array element as a parameter, and return the extracted name so that map() can add it to the new array it's making.我们的 function 需要接受数组元素作为参数,并返回提取的名称,以便map()可以将其添加到它正在制作的新数组中。

function(entry) {
  return entry.accessible_by.name;
}

In the end, we end up with this:最后,我们得到了这样的结果:

var response = JSON.parse(responseBody);

var names = response.entries.map(function(entry) {
  return entry.accessible_by.name;
});

console.log(names);

This will output an array of names to your browser console.这会将 output 名称数组发送到您的浏览器控制台。 ["Test", ...]

Bonus: With a little JavaScript syntactic sugar (specifically arrow functions ), we can make the function code a little cleaner:奖励:使用一点 JavaScript 语法糖(特别是箭头函数),我们可以使 function 代码更简洁:

var response = JSON.parse(responseBody);

var names = reponse.entries.map(entry => entry.accessible_by.name);

console.log(names);

暂无
暂无

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

相关问题 如何从 Postman 测试脚本中的嵌套和变化的 JSON 有效载荷中提取对象/键名称? - How can I extract object/key names from nested and varying JSON payload in a Postman Test script? 如何从数组中提取特定值并将这些值分配给变量以在下一个邮递员请求中使用它 - How do I extract specific value from an array and assigned those value to a variable to use it in next postman request 如何检查邮递员回复中是否没有特定数据? - How do I check that there is no specific data in postman response? POSTMAN - 从 JSON 响应中提取多个值并存储在以逗号分隔的变量中 - POSTMAN - Extract multiple values from a JSON response and store in a variable with comma separated Postman: Extract data from javascript and HTML response into a JSON object - Postman: Extract data from javascript and HTML response into a JSON object 如何编写邮递员测试来将响应 json 与另一个 json 进行比较? - How to write a postman test to compare the response json against another json? 如何从 axios 响应对象中的所有项目中提取特定值到 aa vuex 状态 - How can I extract specific values from all items in axios response object to a a vuex state 我如何从React中的json数据中获取特定值 - How do i get the specific values from json data in React 如何在Postman测试脚本中存储来自系统响应的2个元素组成的数组中的值 - How to store values from an array consisting of 2 elements from a system response in Postman test script Postman - 如何计算 JSON 响应中特定对象的出现次数 - Postman - How to count occurrences of a specific object in a JSON response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM