从空手道框架的响应中提取json对象

时间:2020-02-12 13:25:46

标签: json karate

来自以下API调用的JSON响应

{ 
   "status":200,
   "data":[ 
      { 
         "items":[ 
            { 
               "type":"Notification",
               "content":{ 
                  "message":"Welcome to ACME",
                  "id":"66d93f00-4d74-11ea-9c63-6f69f79cca26",
                  "args":{ 
                     "freezeTimeline":false,
                     "month":0
                  },
                  "timelineButtonText":null
               }
            },
            { 
               "type":"Notification",
               "content":{ 
                  "message":"ACME is a small account.",
                  "id":"670928a0-4d74-11ea-9c63-6f69f79cca26",
                  "args":{ 
                     "freezeTimeline":false,
                     "month":0
                  },
                  "timelineButtonText":null
               }
            },
            { 
               "type":"Event",
               "content":{ 
                  "id":"e08ea760-5b4a-45d2-888e-4a5c8ef5bf1f"
               }
            },
            { 
               "type":"Event",
               "content":{ 
                  "id":"77b0c588-36be-4821-8d26-8c374c29a899"
               }
            },
            { 
               "type":"Event",
               "content":{ 
                  "id":"9fd74f9c-4d50-445b-94f0-37d21e53bdae"
               }
            }
         ],
         "move":{ 
            "Month":1,
            "Year":1,
            "Quarter":1
         }
      }
   ]
}json

Items数组中,type=Event有3个项目 如何获得第一个事件的content.id?

它像下面的代码一样对我有用

And def eventId = response.data[0].items[4].content.id

但是我想要类似的东西:如果有一种“事件”类型,则提取相同的“ content.id”。

1 个答案:

答案 0 :(得分:2)

分两步进行操作,阅读/遵循起来会更容易:

假设items数组位于变量中:

* def events = $items[?(@.type=='Event')]
* def first = events[0].content.id
* match first == 'e08ea760-5b4a-45d2-888e-4a5c8ef5bf1f'