jq

From RaySoft

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. [1]

jq can mangle the data format that you have into the one that you want with very little effort, and the program to do so is often shorter and simpler than you'd expect. [1]

Documentation

Implementations

Syntax

jq [PARAMETER ...] FILTER [FILE ...]

Parameters

-c, --compact-output
By default, jq pretty-prints JSON output. Using this option will result in more compact output by instead putting each JSON object on a single line.
-r, --raw-output
With this parameter, if the filter's result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes. This can be useful for making jq filters talk to non-JSON-based systems.

Examples

Convert the ARP cache list to JSON with jc, remove some attributes and output it nicely with jq
arp -a | jc --arp | jq 'del(.[].name, .[].permanent)'

Output:

[
  {
    "address": "10.0.0.1",
    "hwtype": "ethernet",
    "hwaddress": "0:0:5e:0:1:1",
    "iface": "en9"
  },
  {
    "address": "10.0.0.6",
    "hwtype": "ethernet",
    "hwaddress": "b8:69:f4:1:70:b1",
    "iface": "en9"
  }
]

References

  1. 1.0 1.1 Project contributors. "jq." jq project. https://stedolan.github.io/jq/ (accessed 06.05.2023)