【发布时间】:2019-07-03 01:21:25
【问题描述】:
我想学习如何使用 Lodash 从 JSON 响应中提取变量,因为在其他 Postman 问题中解释的传统方法并没有像我以前在 Jmeter 中使用 json 路径那样解释一个简单的方法。
我需要将以下 json 路径转换为 Lodash 表达式,该表达式返回与此 JSON 路径相同的值
1. FlightSegmentsItinerary[*].Flights[*].Key
2. $..Flights[*].Key
3. Travelers[*].[?(@.TypeCode == "INF")].FirstName (returns the name of the passangers whose type code are == "INF")
JSON 响应:
{
"Travelers": [
{
"TypeCode": "ADT",
"FirstName": "FULANO",
"Surname": "LAZARO",
"Key": "1.1"
},
{
"TypeCode": "INF",
"FirstName": "MENGANO",
"Surname": "XULO",
"Key": "2.2"
}
],
"FlightSegmentsItinerary": [
{
"Flights": [
{
"Key": "1"
},
{
"Key": "2"
}
]
}
]
}
到目前为止,我能够使用以下方法提取旅行者密钥 (Travelers[*].Key):
var jsonData = pm.response.json();
var travelerKeys = _.map(jsonData.Travelers, 'Key');
console.log("travelerKeys: " + travelerKeys);
Output: travelerKeys: 1.1,2.2
如你所见,JSON 路径:
旅行者[*].Key
在 Lodash 中看起来像这样:
var travelerKeys = _.map(jsonData.Travelers, 'Key');
对于这种情况。
【问题讨论】:
-
前 2 个 JSON 路径的结果有什么不同吗,所有 3 个 JSON 路径都会导致数组/多个项目吗?
-
没有 xD,那两个返回相同,我只是想知道获得相同结果的不同方法。是的,它们都应该返回一个包含多个项目的数组