examples.org 2.3 KB

Filter for objects containing specific keys

The following command selects only objects, which contain a key named "result".

cat input.json | jq '. | select(.result != null)' > output.json

Select only specific attributes/keys

The following command will keep only the "text" attribute of every object on the top layer of the input data.

jq '. | {text}' input.json > output.json

Construct objects from attributes selecting attributes conditionally

  • use attr1 in the resulting object unconditionally
  • construct an attribute new_attr
  • the new_attr attribute value will be an array
  • the array of new_attr will contains a selection of array elements of attr2
  • the selection selects all elements, which have a some_key, whose value is < 1
  • use a file as input
  • redirect to an output file

jq '. | {attr1, new_attr: [.attr2 | .[] | select(.some_key < 1)] }' input.json > output.json

Usage:

{ cat <

:results: { "text": "在 这里", "result": [ { "conf": 0.9, "end": 17.13, "start": 16.95, "word": "在" } ] } :end: