Files
vim-ale/jq
2020-07-03 08:34:53 +00:00
..
2020-07-03 06:20:00 +00:00
2020-07-03 06:20:00 +00:00
2020-07-03 06:20:00 +00:00
2020-07-03 08:34:53 +00:00

title, homepage, tagline, description
title homepage tagline description
jq https://stedolan.github.io/jq/ jq is a lightweight and flexible command-line JSON processor. `jq` is like `sed` for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that `sed`, `awk`, `grep` and friends let you play with text.

All jq selectors begin with . - don't forget that!

Be sure to checkout the official tutorial and jq manual for more info.

You can also try online.

How to select a single a property from an object

echo '{ "name": "foo" }' | jq '.name'
"foo"

How to remove quotes from strings

The -r or --raw-output flag unwraps strings:

echo '{ "name": "foo" }' | jq -r '.name'
"foo"

How to select a whole object

echo '{ "name": "foo" }' | jq '.'
{
  "name": "foo"
}

How to select an element from an array

echo '[ { "name": "foo" } ]' | jq '.[0]'
{
  "name": "foo"
}

How to select a single property from an array element

echo '[ { "name": "foo" } ]' | jq -r '.[0].name'
foo

How to select some properties from multiple elements

echo '[ { "name": "foo" }, { "name": "bar" } ]' \
    | jq -r '.[].name'
foo
bar

How transform or zip an array

Anything that doesn't start with a . is part of the transformation template.

Anything that collects starts with .[].

Anything that transforms has a pipe and selector | .whatever.

Be sure to checkout the official tutorial and jq manual for more info.

echo '[ { "name": "foo", "age": 0 }, { "name": "bar", "age": 2 } ]' \
    | jq '{ names: [.[] | .name], ages: [.[] | .age] }'
{
  "names": [
    "foo",
    "bar"
  ],
  "ages": [
    0,
    2
  ]
}