【问题标题】:Rest assured - Invalid JSON expression: Script1.groovy: 1: Unexpected input: '['请放心 - 无效的 JSON 表达式:Script1.groovy:1:意外输入:'['
【发布时间】:2022-01-03 00:06:55
【问题描述】:

我正在尝试使用 JSON 表达式检索具有 "type":"system" 的工作流的 ID

方法:

public static int getSystemWorkflowId(final Map<String, String> cookies) {
        return workflow.get(cookies).then().extract().jsonPath().getInt("$..workflows[?(@.type =='system')].id");
    }

有效载荷

{
    "workflows": [
        {
            "id": 1,
            "name": "Recruitment workflow",
            "type": "system",
            "options": [
                
            ],
            "active": 1
        },
        {
            "id": 3,
            "name": "TestWorkflow",
            "options": [
                
            ],
            "active": 1
        }
    ]
}

错误:

java.lang.IllegalArgumentException:无效的 JSON 表达式: Script1.groovy:1:意外输入:'['@ 第 1 行,第 39 列。 $..workflows[?(@.type =='system')].id

我已经在在线评估器中测试了该表达式,它似乎有效...enter image description here

谢谢!

【问题讨论】:

    标签: rest-assured jsonpath rest-assured-jsonpath json-path-expression


    【解决方案1】:

    Rest-Assured 中的 JsonPath 不是 JsonPath Jayway,请不要混淆。

    Rest-Assured 中的 JsonPath 利用 GPath groovy 来查找和提取价值。

    正确的表达方式是:

    .getInt("workflows.find {it.type == 'system'}.id")
    

    【讨论】:

    • 就是这样。非常感谢!无论如何,你可能知道是否有一些 GPath 表达式的在线评估器?我为 JsonPath 找到了很多,但只为 GPath 找到了一个,这似乎甚至不起作用……似乎 JsonPath 比 GPath 更受欢迎:)
    • 如果你需要 JsonPath 然后把这个依赖添加到你的项目中,不要严格坚持使用 GPath,我在我的项目中都使用了。
    猜你喜欢
    • 2020-08-04
    • 2022-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    相关资源
    最近更新 更多