1

Here is a snippet of the output when I do a basic jq, what I want to get is the data of the partitions.

jq .



[
 {
"partitions": [
  "name@website
],
"os_pid": "20458",
"fd_used": 20,
"fd_total": 1024,
"sockets_used": 2,
"sockets_total": 829,
"mem_used": 41128152,

When I do jq '.partitions' I get Cannot index array with string "partitions" - Any thoughts as to why that happens?

1 Answer 1

2

You have an array, where each element has a partitions field. You ask to get "partitions", but you don't say from which element or elements in the array.

Here's a complete, self-contained file:

[
  {
    "partitions": [ "name@website" ]
  },
  {
    "partitions": [ "more" ]
  }
]

Your expression produces the error you say:

$ jq '.partitions' file
jq: error (at file:8): Cannot index array with string "partitions"

You can get "partitions" for the first element:

$ jq '.[0].partitions' file
[
  "name@website"
]

Or for each elements:

$ jq '.[].partitions' file
[
  "name@website"
]
[
  "more"
]

Or join all the partitions from each element into one list:

$ jq 'map(.partitions) | add' file
[
  "name@website",
  "more"
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.