'How to use jq package to parse name and id from json?

I have an output that i am getting in this format :-

[
  {
    "_class": "hudson.model.FreeStyleProject",
    "name": "my-name",
    "id": "123"
  },
  {
    "_class": "hudson.model.FreeStyleProject",
    "name": "my-name2",
    "id": "456"
  },
  {
    "_class": "hudson.model.FreeStyleProject",
    "name": "my-name3",
    "id": "789"
  }
]

How can i parse the name and id using jq?

I tried to use [].name

but i get curl: (23) Failed writing body (320 != 1338)

Any help will be appreciated. Thank you.

jq


Solution 1:[1]

You failed to mention the relevant error:

jq: error (at <stdin>:17): Cannot index array with string "name"

The program should be

.[].name

Because you provided an incorrect program to jq, it exited earlier than it normally would. This caused the pipe between curl and jq to close, which cause curl to become unable to write to the pipe, which caused curl to emit the error message you did provide.

Solution 2:[2]

Demo https://jqplay.org/s/nolGbk3sD1

Use filter

.[] | .name, .id

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ikegami
Solution 2 Logan Lee