0

I have a string which is comming from API request, that looks like big json each element have 2 fields(name, href), after this action I added this name to array:

str_from_api="$(user_groups_json "limit=100" | jq -r '.items | .[].name')"
readarray -t usergroups_array <<< "$str_from_api"

But I need to store 2 variables(name and href) in array usergroups_array. I mean next solution:

Array:

str_from_api=(name:"Test name", href: "Test href")

This array will be long with 100 records. After I need to do operations with each name and each href, that is why I need access to each element of this structure.

How can I implement this?

UPDATE

json:

{
 "totalCount": 2,
 "items": [
     {
      "name": "test name",
      "active": false,
      "createdFrom": "SAML",
      "_meta": {
        "allow": [
        "DELETE",
        "GET",
        "PUT"
       ],
       "href": "https://href2.com"
  }
},
{
  "name": "test name 2",
  "active": false,
  "createdFrom": "SAML",
  "_meta": {
    "allow": [
      "DELETE",
      "GET",
      "PUT"
    ],
    "href": "https://href1.com"
  }
}
]
Community
  • 1
  • 1

1 Answers1

0

Here's a quick and dirty attempt at getting your output in a Bash loop.

user_groups_json "limit=100" |
jq -r '.items | .[] | (._meta.href + " " + .name)' |
while read -r href name; do
    echo "'$name' has href '$href'"
    : do stuff with these variables here
done

You say you want these in an array, but then explain that you wish to process them one at a time; so storing the whole list in a variable which you only access once seems superfluous.

tripleee
  • 175,061
  • 34
  • 275
  • 318