Parse output JSON using GPATH not working- Help needed please

193 views
Skip to first unread message

Ammu

unread,
Jan 12, 2016, 1:57:07 PM1/12/16
to REST assured
Given below JSON, I need to retrieve all columnList.metricColumnName for a given metricGroupName. Please help. I tried all possible JSONPath, not working.

String grp="group1";
tried this- List<Map<String, ?>> metrics =from(jsonOutput).get("metricGroupTypeList[0].columnList*.findAll {metricGroupName =='"+grp+"'}");

JSON as below:

{
    "entityType": "type1",
    "typeDisplayName": "Type1",
    "parentTargetType": "ParentType1",
    "meClass": "TARGET",
    "category": "Others",
    "metricGroupTypeList": [
        {
            "entityTypeName": "type1",
            "metricGroupDisplayName": "group1",
            "metricGroupName": "group1",
            "config": false,
            "displayInUI": true,
            "parentMGName": null,
            "fullPath": "group1",
            "columnList": [
                {
                    "metricColumnDisplayName": "column1",
                    "metricColumnName": "column1",
                    "metricColumnClass": "NUM",
                    "typeFormat": null,
                },
{
                    "metricColumnDisplayName": "column2",
                    "metricColumnName": "column2",
                    "metricColumnClass": "NUM",
                    "typeFormat": null,
                }
            ],
            "description": null,
            "stm": true,
            "curationLevel": 1,
            "extension": false,
            "keyColumnNames": []
        },
        {
            "entityTypeName": "type2",
            "metricGroupDisplayName": "group2",
            "metricGroupName": "group2",
            "config": false,
            "displayInUI": true,
            "parentMGName": null,
            "fullPath": "group2",
            "columnList": [
                {
                    "metricColumnDisplayName": "column1",
                    "metricColumnName": "column1",
                    "metricColumnClass": "NUM",
                    "typeFormat": null,
                }
            ],
            "description": null,
            "stm": true,
            "curationLevel": 1,
            "extension": false,
            "keyColumnNames": []
        }
]
}

Johan Haleby

unread,
Jan 13, 2016, 2:24:54 AM1/13/16
to rest-a...@googlegroups.com
Probably something like this:

List<String> metricColumnNames = from(jsonOutput).get("metricGroupTypeList.find { it.metricGroupName == 'group1' }.columnList.metricColumnName");

Replace 'group1' with the group you're looking for.

/Johan

--
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.
For more options, visit https://groups.google.com/d/optout.

Andrei Stoica

unread,
Jan 14, 2016, 3:12:44 AM1/14/16
to REST assured
You can also try this in order to use the variable grp and not a hardcoded string:

String grp = "group1";
List<String> metricColumnNames = from(jsonOutput).get("metricGroupTypeList.find { it.metricGroupName == '%d' }.columnList.metricColumnName",
withArgs(grp ));

Hope it helps.

Vinithra

unread,
Feb 21, 2016, 10:33:32 AM2/21/16
to rest-a...@googlegroups.com
Hi Johan, Andrei,

Thanks a lot for the inputs.

I am trying the same formula to get the corresponding metricColumnClass for a particular metricColumnName, but doesn't work.
What is the syntax for the next level in JSON.

String class = from(jsonOutput).get("columnList.find {it.metricColumnName== 'column1'}.metricColumnClass");

Also, great if you could help with generic JSONPath tutorials/examples if any. 

--
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.
For more options, visit https://groups.google.com/d/optout.



--
Thanks and Regards,
Vinithra S P
---------------------------------------------------------------------------------------------------
"Today is a gift , that is why it is called the present."

"Smile - A curve that can set a lot of things straight" :)
---------------------------------------------------------------------------------------------------

Vinithra

unread,
Feb 21, 2016, 11:21:10 AM2/21/16
to rest-a...@googlegroups.com
also withArgs(grp )); option doesn't seem to work.

I even tried param which doesn;t wor' either.

Vinithra

unread,
Feb 21, 2016, 11:31:09 AM2/21/16
to rest-a...@googlegroups.com
I followed the example in  rest assured unit test, ,which doesn't work either.

https://github.com/jayway/rest-assured/blob/master/json-path/src/test/java/com/jayway/restassured/path/json/JsonPathTest.java

String class= from(jsonOutput).get("metricGroupTypeList.columnList.find {columnList -> columnList.metricColumnName == 'column1'}.metricColumnClass"); 

Please help point out what is wrong with above path.

Johan Haleby

unread,
Feb 21, 2016, 12:54:34 PM2/21/16
to rest-a...@googlegroups.com
Add flatten to your path:

"metricGroupTypeList.columnList.flatten().find {columnList -> columnList.metricColumnName == 'column1'}.metricColumnClass"

The reason is that "metricGroupTypeList.columnList" returns a list of "columnList" which is also a list. So if we flatten them we only have a non-nested list to deal with.

Reply all
Reply to author
Forward
0 new messages