2
{
    "success": true,
    "code": 200,
    "message": "Success",
    "payload": {
        "data": [
            {
                "_id": "123",
                "state": "Archived",
                "version": "2",
                "name": "Sample",
                },
                "__v": 0
            },
            {
                "_id": "456",
                "state": "Active",
                "version": "3",
                "name": "Sample1",          
                },
                "__v": 0
            },
            {
                "_id": "789",
                "state": "Draft",
                "version": "DRAFT",
                "name": "Testing11",                
                },
                "__v": 0
            },
            {
                "_id": "5d970df79eb6df005d115e2a",
                "state": "Active",
                "version": "1",
                "name": "Tester111",               
                },
                "__v": 0
            },

        ],

    },
    "error": [],
       "datetime": "2019-10-08T11:49:57.122Z",
    "version": "1.0.0"
}

I need to find the id of which have active status and particular name. Then I need to compare them in my assertion. I am having hard time fetching the id off. I am using JsonPath to get this thing done. And here's my Json path expression.

I am using following jsonpath expression -

String idValue = from(response).getList("payload.data[?(@.state=='Active' && @.name=='SampleAssessment')]._id").toString();

but getting error as -

java.lang.IllegalArgumentException: Invalid JSON expression: Script1.groovy: 1: Unexpected input: '
payload.data[?' @ line 1, column 40. payload.data[?(@.state=='Active' && @.name=='SampleAssessment')]._id

    ^

1 error

bummi
  • 27,123
  • 14
  • 62
  • 101
pramod
  • 21
  • 1

1 Answers1

0

This syntax doesn't work with RestAssured unfortunately, you need to use GPath syntax instead.

Something like this should work

"payload.data.find { node -> node.get('state') == 'Active' && node.get('name') == 'SampleAssessment' }"
Justin Rowe
  • 2,356
  • 1
  • 20
  • 15