我们可以选择使用空手道脚本过滤数据吗? [英] Can we have option to filter the data using Karate scripts?

查看:60
本文介绍了我们可以选择使用空手道脚本过滤数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON响应:

I have below JSON response:

{
        "name": "Cabinets, Enclosures and Racks",
        "parentId": "",
        "categoryId": "55040",
        "count": "3291",
        "categoryLevel": "TOP"
    },
    {
        "name": "Cable Management and Accessories",
        "parentId": "55040",
        "categoryId": "55041",
        "count": "573",
        "categoryLevel": "MIDDLE"
    },
    {
        "name": "Cable Management",
        "parentId": "55041",
        "categoryId": "55042",
        "count": "467",
        "categoryLevel": "BOTTOM"
    },

我需要获取唯一的TOP CategoryLevel数据.有什么选择吗

I need to fetch the only TOP CategoryLevel data. Is there any option to do so

推荐答案

是的,为什么您不仔细阅读文档:

Yes, why don't you read the docs a little more carefully: https://github.com/intuit/karate#jsonpath-filters

尝试将以下内容粘贴到新的Scenario中,然后查看其工作原理:

Try pasting the below in a fresh Scenario and see it working:

* def response = 
"""
[
   {
      "name":"Cabinets, Enclosures and Racks",
      "parentId":"",
      "categoryId":"55040",
      "count":"3291",
      "categoryLevel":"TOP"
   },
   {
      "name":"Cable Management and Accessories",
      "parentId":"55040",
      "categoryId":"55041",
      "count":"573",
      "categoryLevel":"MIDDLE"
   },
   {
      "name":"Cable Management",
      "parentId":"55041",
      "categoryId":"55042",
      "count":"467",
      "categoryLevel":"BOTTOM"
   }
]
"""
* def top = $response[?(@.categoryLevel=='TOP')]
* match top[0] == { name: 'Cabinets, Enclosures and Racks', parentId: '', categoryId: '55040', count: '3291', categoryLevel: 'TOP' }

这篇关于我们可以选择使用空手道脚本过滤数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆