Jq Usage: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 38: Line 38:
<syntaxhighlight lang='json'>
<syntaxhighlight lang='json'>
{
{
   "complex::key": "something"
   "complex::color": "something"
}
}
</syntaxhighlight>
</syntaxhighlight>


  jq '.complex::key'
  jq '.complex::color'


will fail:
will fail:


  jq: error: syntax error, unexpected ':', expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
  jq: error: syntax error, unexpected ':', expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
  .complex::key
  .complex::color


while  
while  


  jq '.["complex::key"]'  
  jq '.["complex::color"]'  


will work.
will work.


=Array Filters=
=Array Filters=

Revision as of 18:44, 1 March 2019

Internal

Command Line Options

-r

Output raw strings, not JSON content.

Universal Filters

Identity Filter .

Map Filters

Object Identifier-Index Filter .key

.key

The filter expects a map and produces the value associated with the key given as argument of the filter, or null if there is no such object. The value, if exists, can be a primitive, a map or an array.

cat example.json | jq '.color'

This syntax only works for "identifier-like" keys: keys that are all made of alphanumeric characters and underscore, and which do not start with a digit.

The .key syntax is actually an alias for the more generic syntax:

.["key"]

If the key contains special characters, the .key alias cannot be used, and the complete .["key"] syntax should be used:

jq '.["complex::key"]'

For:

{
  "complex::color": "something"
}
jq '.complex::color'

will fail:

jq: error: syntax error, unexpected ':', expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.complex::color

while

jq '.["complex::color"]' 

will work.

Array Filters