How to retrieve values from json object using awk or sed
Using jq
:
$ printf '%s\n' "$Group_ID" | jq '.[] | select(.name == "Admin_UserGroup")'
{
"id": "89f3bd6a-33a9-4e02-9fe3-eae660c5a6cf",
"name": "Admin_UserGroup",
"path": "/Admin_UserGroup",
"subGroups": []
}
This selects all objects in the array whose name
key corresponds to a value of Admin_UserGroup
.
According to your consideration for basic commands, if your file has structure exactly as shown, grep
may help you:
$ grep -B2 -A3 '"name" : "Admin_UserGroup"' File
but if all in one line:
$ sed -E 's/\},\s*\{/\},\n\{/g' File | grep '"name" : "Admin_UserGroup"'
Using your original example, you'd use my solution like so:
$ echo "$Group_ID" | grep ...
You can use JSON.awk:
awk -f JSON.awk -v file1.json file2.json
https://github.com/step-/JSON.awk