5

My need: I want to build a path containing filters on my JSON response.

//Simple path - OK
response.Node1.Node2.Node3.Node4

//Path with list - OK
response.Node1.Node2.Node3[1].Node4

//Path with condition (filter) - NOK
response.Node1.Node2[?(@.Node3 == 'value')].Node3bis

Postman does not understand the [?(@.Node3 == 'value')] syntax as jsonPath is not supported natively.

Hence, I am trying to import the jsonPath js library in Postman, and I have found two:


Researches

  • I already read this post, and none of the answers satisfy my needs.

  • Postman js code is limited to the sandbox.

  • Postman limits libraries to this list.

  • There is a trick (check TIP #5) to add js code as global variable, and call it in requests. However it does not work for jsonPath project as it is not just a simple function but a whole project. I tried many different ways, but still unable to call the jsonPath functions from my Postman Test.

  • I also read the related Postman issue.

  • From GitHub community, this issue was closed in favor of this one. Which has been closed in favor of this one. Which is opened without clear answer (only small tricks mentioned above) since June 2015.


Unknown domains

Even if I read their global use, I do not know anything technical about NodeJs / NPM / Newman.

But I read that postman supports NodeJs, allows to generate NodeJs code snippet and I saw that jsonPath library can be imported using NodeJs.

Maybe something can be done from there, but I don't have the expertise to make it happen.


Question:

Does anyone knows how to make it work altogether ?

I mean, find a way to use jsonPath expression, thanks to the jsonPath js library, in postman... Please help.

Any postman collection making it work is welcome :).

4

1 回答 1

2

关于第一个问题,目前不支持导入外部 js 代码(除了上面透露的全局变量技巧),但 Postman 团队正在开发中。如果使用金丝雀版本,您可以在正式交付之前获得它。

关于我的需要,我实际上找到了一种使用Cheerio建立有条件的路径的方法。注意:responseBody 是 XML 格式的,因此我会加载cheerio。

var $ = cheerio.load(responseBody, {
  ignoreWhitespace: true,
  xmlMode: true
});

//Will show in the console the value of NODE3bis corresponding to a NODE3='value'
console.log("cheerio xpath with condition: " + 
$('NODE1 > NODE2 > NODE3:contains("value")')
    .parent().find('NODE3bis').text());
//Where NODE3 and NODE3bis are sibling. 

//Another way to write it using .has() function, which allows to avoid the use of .parent() afterward:
console.log("cheerio xpath with condition: " + 
$('NODE1 > NODE2').has('NODE3:contains("value")').find('NODE3bis').text());
于 2019-02-19T21:37:28.670 回答