【问题标题】:How to get only few elements from the Array List of Objects in karate如何从空手道中的对象数组列表中仅获取少数元素
【发布时间】:2020-09-11 19:30:26
【问题描述】:

我有以下 json:

{
    "store": { 
        "book": [ { 
            "category": "reference", 
            "author": "Nigel Rees", 
            "title": "Sayings of the Century", 
            "price": 8.95 
        }, { 
            "category": "fiction", 
            "author": "J. R. R. Tolkien", 
            "title": "The Lord of the Rings", 
            "isbn": "0-395-19395-8", 
             "price": 22.99 
        } ], 
        "bicycle": { 
            "color": "red", "price": 19.95 
        } 
    }, 
    "expensive": 10 
}

我想通过以下方式检索所有books'authorcategory

[ { 
    "category": "reference", 
    "author": "Nigel Rees" 
}, { 
    "category": "fiction", 
    "author": "J. R. R. Tolkien" 
} ]

【问题讨论】:

    标签: karate jsonpath


    【解决方案1】:

    请阅读文档:https://github.com/intuit/karate#json-transforms

    * def fun = function(x){ return { category: x.category, author: x.author } }
    * def books = karate.map(response.store.book, fun)
    

    【讨论】:

    • 上述解决方案按预期工作并解决我的目的.... [ { "category": 7, "email": "michael.lawson@reqres.in" }, { "category": 8 , "电子邮件": "lindsay.ferguson@reqres.in" }]
    • @UdayVangala 很好,请阅读:stackoverflow.com/help/someone-answers
    猜你喜欢
    • 1970-01-01
    • 2016-04-01
    • 2017-04-18
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    相关资源
    最近更新 更多