J. Groesser
J. Groesser

Reputation: 3047

Extracting multiple fields from a JSON payload

I have the following payload

[
  {
    "name": "ProductCostingBJF_v2.3.0",
    "commit": {
    },
    "protection": {
      "enabled": true,
      "required_status_checks": {
        "enforcement_level": "off",
        "contexts": []
      }
    }
  },
  {
    "name": "master",
    "commit": {
    },
    "protection": {
      "enabled": true,
      "required_status_checks": {
        "enforcement_level": "off",
        "contexts": []
      }
    }
  }
]

can you help with the jq syntax to pull out and present

ProductCostingBJF_v2.3.0    true
master                      true

I got the first part ... I can get the branch name with

jq  -r .[].name

but i cannot figure how to get the enabled value.

Upvotes: 2

Views: 329

Answers (2)

kenorb
kenorb

Reputation: 166813

Here is alternative syntax using joined lines (-j):

$ jq -j '.[] | .name, "\t", .protection.enabled, "\n"' payload.json 
ProductCostingBJF_v2.3.0    true
master  true

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 158180

You can use the following jq command:

jq -r '.[]| "\(.name) \(.protection.enabled)"' a.json 

You can pipe it to column to get aligned output:

jq -r '.[]| "\(.name) \(.protection.enabled)"' a.json | column -t

Output:

ProductCostingBJF_v2.3.0  true
master                    true

Upvotes: 1

Related Questions