Extracting information from JSON response

411 views
Skip to first unread message

bswji...@gmail.com

unread,
May 30, 2021, 2:53:03 AM5/30/21
to REST assured
Hi Team,

Good Morning,  I am facing an issue from a long time:

JsonPath values = myPermissionResponse.jsonPath();
String a = values.get("$.items.[?(@.workProgramType=='Itemized')].id");
System.out.println(a);

Getting an error while execution: 

java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unexpected token: [ @ line 1, column 35.
                            $.items.[?(@.workProgramType=='Itemized')].id
                                     ^

1 error

My Question comes hear-- when I run in JSON path online validator it is working fine but the same when use in script (i.e. RestAssured) it is throwing an error.

NOTE: I am aware RestAssured internally use Gson but I have tried multiple resource could not land any solution

My Complex JSON looks:

{
    "schemaVersion": 1,
    "methodologyId": "609b8ed9b46b1703e65aa0ed",
    "revisionId": "60a6376eb46b1703e65abf30",
    "rootOrder": [
        "609b8eedaa642c3ea70c5404",
        "609e2731aa642c3ea70c6311",
        "60a63627aa642c3ea70c70ea"
    ],
    "items": {
        "609b8eedaa642c3ea70c5404": {
            "itemType": "Folder",
            "childOrder": [
                "609bc9a9aa642c3ea70c5753",
                "609b8ef3aa642c3ea70c5406"
            ],
            "data": {
                "itemType": "Folder",
                "hidden": null,
                "id": "609b8eedaa642c3ea70c5404"
            }
        },
        "609b8ef3aa642c3ea70c5406": {
            "parentId": "609b8eedaa642c3ea70c5404",
            "itemType": "WorkProgram",
            "childOrder": [
                "609e26bcaa642c3ea70c630f",
                "609e26abaa642c3ea70c6309",
                "609bc993aa642c3ea70c574f",
                "609b8f00aa642c3ea70c5408"
            ],
            "data": {
                "itemType": "WorkProgram",
                "hidden": null,
                "workProgramType": "QuestionAnswer",
                "tailoring": "Allowed",
                "visibility": "Show",
                "ruleContextType": "Standard",
                "id": "609b8ef3aa642c3ea70c5406"
            }
        }
}

how can I parse to get the desire output .

Ravi Ranjan

unread,
May 30, 2021, 3:08:35 AM5/30/21
to rest-a...@googlegroups.com
Pass token in json application formate.. the way we do in postman

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rest-assured/f553b4b1-8857-479b-a4d4-a0c2dc8abeeen%40googlegroups.com.


--
Thanks & Regards,
Ravi Ranjan Pandey

bswji...@gmail.com

unread,
May 30, 2021, 7:45:28 AM5/30/21
to REST assured
Thanks @Ravi, 

can you explain a bit more on the approach .. where I am making mistake and how to update the "$.items.[?(@.workProgramType=='Itemized')].id" to retrive the desire output.

bswji...@gmail.com

unread,
May 30, 2021, 11:40:27 AM5/30/21
to REST assured
Hi All,


I got the solution:

items.collect { it.value }.reverse().data.find{it.workProgramType=='Itemized'}.id
it might help other.
Reply all
Reply to author
Forward
0 new messages