1

I'm trying to retrieve the id of the workflow having "type":"system" using JSON expression

Method:

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

Payload

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

Error:

java.lang.IllegalArgumentException: Invalid JSON expression: Script1.groovy: 1: Unexpected input: '[' @ line 1, column 39. $..workflows[?(@.type =='system')].id

I've tested the expression in an online evaluator and it seems to work...enter image description here

Thanks!

lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20
Txili
  • 17
  • 3

1 Answers1

1

JsonPath in Rest-Assured is not JsonPath Jayway, please don't get confused.

JsonPath in Rest-Assured utilizes GPath groovy to find and extract value.

The correct expression would be:

.getInt("workflows.find {it.type == 'system'}.id")
lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20
  • That was it. Thanks a lot! Anyway, Do you maybe know if there is some online evaluator for GPath expressions? I found quite a few for JsonPath, but only one for GPath, tthat doesn't even seem to work... It seems that JsonPath if far more popular that GPath :) – Txili Nov 26 '21 at 08:21
  • If you need JsonPath then add this dependency to your project, no strict to stick with GPath, I use both in my project. – lucas-nguyen-17 Nov 26 '21 at 08:38