1

What i did?

[root@mdfdevha1 ~]# Group_ID=`/opt/keycloak/bin/kcadm.sh get groups -r TEST`

[root@mdfdevha1 ~]# echo $Group_ID
[ { "id" : "57c86153-11ba-4d19-aafb-9903ee00086b", "name" : "Admin_UserGroup", "path" : "/Admin_UserGroup", "subGroups" : [ ] } ]
[root@mdfdevha1 ~]# SURE_USER_ID=`echo $Group_ID | cut -d : -f2 | awk -F\" '{print $2}'`
[root@mdfdevha1 ~]# echo $SURE_USER_ID
57c86153-11ba-4d19-aafb-9903ee00086b

In this case i am able to get the id but my requirement little bit complex I run below command

[root@mdfdevha1 ~]#  Group_ID=`/opt/keycloak/bin/kcadm.sh get groups -r My_Realm`

and then

[root@mdfdevha1 ~]# echo $Group_ID

Output was multiple record in JSON format

[ { "id" : "e27206c0-aeb6-43db-acda-c4ba43233071", "name" : "A1", "path" : "/A1", "subGroups" : [ ] }, 
{ "id" : "89f3bd6a-33a9-4e02-9fe3-eae660c5a6cf", "name" : "Admin_UserGroup", "path" : "/Admin_UserGroup", "subGroups" : [ ] }, 
{ "id" : "cdc2bce5-c3bb-4b88-bdaf-d87b8bb6c644", "name" : "Group104", "path" : "/Group104", "subGroups" : [ ] }, 
{ "id" : "a0d749f2-ab6c-4c27-ad55-3357eaab9527", "name" : "Group105", "path" : "/Group105", "subGroups" : [ ] }, 
{ "id" : "fbf99c34-d50d-408b-8d19-9713f9af3e3a", "name" : "Group106", "path" : "/Group106", "subGroups" : [ ] }, 
{ "id" : "ebd8336f-4017-4fb1-8035-153ae1d9ba37", "name" : "Group201", "path" : "/Group201", "subGroups" : [ ] }, 
{ "id" : "38f4aef7-caf0-4430-9e61-1ae7026e872f", "name" : "Group202", "path" : "/Group202", "subGroups" : [ ] }, 
{ "id" : "436a0f4a-8b1b-4d7d-a014-fcec3513644e", "name" : "Group203", "path" : "/Group203", "subGroups" : [ ] }, 
{ "id" : "41962c5f-e7e9-4748-b81f-e3f1880b78de", "name" : "Sure_Groups", "path" : "/Sure_Groups", "subGroups" : []}]

Now from the above output i want to get the ID where name = Admin_UserGroup.

How can this will achieve ?

Kusalananda
  • 333,661
subodh
  • 291

3 Answers3

3

You could use a language that comes bundled with a JSON parsing library:

echo "$Group_ID" | python -c '
  import fileinput
  import json

  json_data = ""
  for line in fileinput.input():
    json_data = json_data + line

  data = json.loads(json_data)
  for o in data:
    if o["name"] == "Admin_UserGroup":
      print o["id"]
'
89f3bd6a-33a9-4e02-9fe3-eae660c5a6cf
glenn jackman
  • 85,964
2

The id string for the Admin_UserGroup may be had with

Group_ID=$( /opt/keycloak/bin/kcadm.sh get groups -r TEST )
SURE_USER_ID=$( printf '%s' "$Group_ID" | jq -r '.[]|select(.name == "Admin_UserGroup").id' )

If you don't need Group_ID for anything else:

SURE_USER_ID=$( /opt/keycloak/bin/kcadm.sh get groups -r TEST | jq -r '.[]|select(.name == "Admin_UserGroup").id' )

This uses jq to parse the JSON document document with a simple query that returns the id field for the array entries whose name is equal to Admin_UserGroup.

Related:

Kusalananda
  • 333,661
  • Thanks for your answer ,in my case i am getting jq not found ,do i have to install some other library ? Is this possible to get the data without jq help? – subodh Jul 31 '18 at 05:37
  • @SubodhJoshi You would have to install the jq program. Use your ordinary package manager to locate and install it. – Kusalananda Jul 31 '18 at 05:40
  • Is this possible to do it without use of this library . – subodh Jul 31 '18 at 06:27
  • @SubodhJoshi Yes, there are several other libraries for parsing JSON available. I'm don't have the time right now to write another answer though. I'm sure someone will come up with same awk or sed answer that might work. – Kusalananda Jul 31 '18 at 06:35
2

If there's no tool or library for json parsing installed and you can't install any or can't use any portable one, if if you can make some assumptions on the format of the json file (like it's always one space on either side of the :, there's no escaped character, no {, } in string values, no nested {...} ...) as seems it would be possible from your sample:

/opt/keycloak/bin/kcadm.sh get groups -r My_Realm |
  perl -l -0777 -ne 'for (/\{.*?\}/sg) {
    print $1 if /"name" : "Admin_UserGroup"/ && /"id" : "(.*?)"/}'

For a more bullet-proof version:

perl -l -0777 -ne 'while(m(([^{}"]++|"(?:\\.|[^\\"])*+"|
  \{(?:"name"\s*:\s*("Admin_UserGroup")|"id"\s*:\s*"((?:\\.|[^\\"])*+)"|
  (?1))*\}))gxs) {print $3 if $2}'