I want to extract a specific string with the jq command.

Asked 1 years ago, Updated 1 years ago, 413 views

I would like to get the id 7721291801 from here with the jq command.

{
    "total_count": 1,
        "entries": [
        {
            "type": "group",
            "id": "7721291801",
            "name": "test-group 1",
            "group_type": "managed_group"
        }
        ],
        "limit": 100,
        "offset"—0
}

The following command results in an error:

commands executed:

 curl-i-X GET "https://api.box.com/2.0/groups" - H "Authorization: Bear xxxxxxxxx" | jq-R'fromjson? '|jq'.[].id'

error messages:

jq:error(at<stdin>:13):Cannot index number with string "id"

Please tell me how to extract the ID value.

json jq

2022-09-30 21:57

2 Answers

entries If you want to take out the following, it should be jq'.entries[]|.id' as stated in the comment.
If you want to extract all the id's from the child elements of an indefinite hierarchy, you can find them in jq'..|.id?|select(.!=null)'.

Note: Access JSON's deep hierarchy without worrying about it


2022-09-30 21:57

I passed the file from the standard input and checked it.
|jq.. or later.

cat<<EOF|jq-r'.entries[].id'
{
  "total_count": 1,
  "entries": [
    {
      "type": "group",
      "id": "7721291801",
      "name": "test-group 1",
      "group_type": "managed_group"
    }
  ],
  "limit": 100,
  "offset"—0
}
EOF

The -r option should be added.


2022-09-30 21:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.