使用Postman中的JS Lodash提取嵌套的JSON数组响应对象

时间:2019-07-03 01:21:25

标签: json postman lodash

我想学习如何使用Lodash从JSON响应中提取变量,因为在其他邮递员问题上解释的传统方法无法解释实现此目的的简便方法,因为我以前在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路径:

  

旅行者[*]。密钥

在Lodash中看起来像这样:

  

var travelerKeys = _.map(jsonData.Travelers,'Key');

在这种情况下。

2 个答案:

答案 0 :(得分:1)

var jsonData = {
  "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"
      }
    ]
  }]
}

// 1. FlightSegmentsItinerary[*].Flights[*].Key 
console.log( _(jsonData.FlightSegmentsItinerary).flatMap('Flights').map('Key') ) 

//2. $..Flights[*].Key
console.log( _.chain(jsonData).values().flatten().find('Flights').values().flatten().map('Key') )

//3. Travelers[*].[?(@.TypeCode == "INF")].FirstName (returns the name of the passangers whose type code are == "INF")
console.log( _(jsonData.Travelers).filter(['TypeCode', 'INF']).map('FirstName') )
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>


另一种选择可能是尝试使用JavaScript库,例如https://github.com/dchester/jsonpath

var jsonData = {
  "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"
      }
    ]
  }]
}

console.log(jsonpath.query(jsonData, '$.FlightSegmentsItinerary[*].Flights[*].Key'))

console.log(jsonpath.query(jsonData, '$..Flights[*].Key'))

console.log(jsonpath.query(jsonData, '$.Travelers..[?(@.TypeCode == "INF")].FirstName')) 
<script src="https://cdn.jsdelivr.net/npm/jsonpath@1.0.2/jsonpath.min.js"></script>

由于Postman不支持fetchXMLHttpRequest,因此jsonpath.min.js文件的内容可以保存在环境变量中,然后保存在eval(pm.environment.get('jsonpath'));中,如中所述。 https://community.getpostman.com/t/adding-external-libraries-to-postman/1971/4

答案 1 :(得分:0)

您必须使用require函数(请参见以下代码)来告诉Postman您要使用哪个沙盒模块。您收到的错误与邮递员有关,其中有些问题无法解决Here they are talkingHere is the postman issue tracker

我尝试为我工作的代码

const moment = require('lodash');
var keys = _.chain(obj.Travelers)
        .map("Key")
        .flatten()
        .unique()
        .value();
console.log(keys);

输出

Array:[]
0 : "1.1"
1 : "2.2"

形成更多详细信息,您可以查看
Postman Sandbox API reference
postman-and-lodash-the-perfect-partnership