I'm using Gatling to test API's and I have to extract an ID from a JSON and store it in a variable, but Gatling doesn't support this: "$.[?(@.name == 'manage-account')].id"
How can I find the ID that belongs to 'manage-account'?
The JSON looks like this:
{
"realmRoles" : [ {
"id" : "2253a9b4-bc1e",
"name" : "offline_access",
"description" : "${role_offline-access}",
"composite" : false,
"clientRole" : false,
"containerId" : "sp"
}, {
"id" : "313d800c",
"name" : "ua_authorization",
"description" : "${role_ua_authorization}",
"composite" : false,
"clientRole" : false,
"containerId" : "sp"
} ],
"clientRoles" : [ {
"client" : {
"id" : "37e4837b-8e77",
"name" : "account"
},
"roles" : [ {
"id" : "48abb872-dc25",
"name" : "manage-account-links",
"description" : "${role_manage-account-links}",
"composite" : false,
"clientRole" : true,
"containerId" : "37e4837b-8e77e"
}, {
"id" : "a1fadc14-30c18",
"name" : "manage-account",
"description" : "${role_manage-account}",
"composite" : true,
"clientRole" : true,
"containerId" : "37e4837b-8e77"
} ]
}, {
"client" : {
"id" : "ef80d8e9-5f9b",
"name" : "realm-management"
},
"roles" : [ {
"id" : "d1e35e29-b26e",
"name" : "manage-users",
"description" : "${role_manage-users}",
"composite" : false,
"clientRole" : true,
"containerId" : "ef80d8e9-5f93"
} ]
}]
} ]
}
JsonPath currently lacks a proper spec, so syntax interpretation differs from one implementation to another.
Here, you should be using $..*[?(@.name == 'manage-account')].id
: you want recursive descent (..
) and match any node (*
) that matches your filter.
If you want to stop suffering from those differences, I would recommend switching to JMESPath instead, see our blog. The main thing JMESPath lacks comparing to JsonPath is recursive descent, which is convenient but has poor performance. You have to specify the full path.