Skip to content

jq / yq

Base actions

1
2
3
4
$ echo '{"foo": "bar"}' | jq .
{
  "foo": "bar"
}
$ echo '{"foo": "bar"}' | jq '.foo'
"bar"
$ echo '{"foo": "bar"}' | jq -r '.foo'
bar
echo '[{"foo": "bar"}, {"foo": 2}, {"foo": "kowabunga"}]' | jq .
[
  {
    "foo": "bar"
  },
  {
    "foo": 2
  },
  {
    "foo": "kowabunga"
  }
]
$ echo '[{"foo": "bar"}, {"foo": 2}, {"foo": "kowabunga"}]' | jq '.[]'
{
  "foo": "bar"
}
{
  "foo": 2
}
{
  "foo": "kowabunga"
}
1
2
3
4
$ echo '[{"foo": "bar"}, {"foo": 2}, {"foo": "kowabunga"}]' | jq '.[0]'
{
  "foo": "bar"
}
1
2
3
4
$ echo '[{"foo": "bar"}, {"foo": 2}, {"foo": "kowabunga"}]' | jq '.[2]'
{
  "foo": "kowabunga"
}
$ echo '[{"foo": "bar"}, {"foo": 2}, {"foo": "kowabunga"}]' | jq '.[2].foo'
"kowabunga

Select

Find in array:

Find based on key ("in this array, print the ID of the user "bob"):

$ echo '[ {"id": 1, "name": "alice"}, {"id": 2, "name": "bob"} ]' | jq '.[] | select(.name == "bob") | .id'
2

Find based on key in array ("in this array, print the ID of all dict having alice in its users"):

1
2
3
$ echo '[ {"id": 1, "users": ["alice", "bob"]}, {"id": 2, "users": ["alice"]}, {"id": 3, "users": ["bob"]} ]' | jq '.[] | select(.users[] | contains("alice")) | .id'
1
2

Append

Add key to dict:

1
2
3
4
5
$ echo '{"foo": "kowabunga"}' | jq '. + {"bar": "new item"}'
{
  "foo": "kowabunga",
  "bar": "new item"
}

It can override existing keys:

1
2
3
4
echo '{"foo": "kowabunga"}' | jq '. + {"foo": "new item"}'
{
  "foo": "new item"
}

Add item to array:

echo '[{"foo": "bar"}, {"foo": 2}, {"foo": "kowabunga"}]' | jq '. + [{"foo": "new item"}]'
[
  {
    "foo": "bar"
  },
  {
    "foo": 2
  },
  {
    "foo": "kowabunga"
  },
  {
    "foo": "new item"
  }
]

Useful aliases

Flatten YAML:

$ alias flatten_yaml='yq -y '\''[tostream | select(has(1)) | first |= join(".") | {key: first, value: last}]| from_entries'\'

$ cat file.yaml
image:
  repository: prom/prom-kpi
  pullPolicy: IfNotPresent
  tag: ""
imagePullSecrets: []
replicaCount: 1
...

$ flatten_yaml file.yaml
image.repository: prom/prom-kpi
image.pullPolicy: IfNotPresent
image.tag: ''
imagePullSecrets: []
replicaCount: 1
...

Note about yq and yq

There are two distincts yq packages: