Jq Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(12 intermediate revisions by the same user not shown)
Line 6: Line 6:
=Pipeline Content=
=Pipeline Content=


The pipeline flows primitives, maps and arrays.
The pipeline flows items that are: primitives, maps and arrays.


Individual strings flowing on a pipeline can be simulated with:
Individual strings flowing on a pipeline can be simulated with:
Line 29: Line 29:


An iteration is pipeline content produced by the [[Jq_Usage#Array-to-Iterator_Filter_.5B.5D|.[] filter]], that converts and array into its individual elements.
An iteration is pipeline content produced by the [[Jq_Usage#Array-to-Iterator_Filter_.5B.5D|.[] filter]], that converts and array into its individual elements.
=Pipe Operator |=
The pipe operator '|' combines two filters by feeding the output of the filter specified on the left into the input of the filter specified on the right.
If the left filter produces an [[#Iteration|iteration]], the filter on the right is applied to each of the iteration elements.
Usually the "|" can be omitted on command line and it can be implied:
<syntaxhighlight lang='java'>
jq '.a.b.c'
</syntaxhighlight>
is equivalent with:
<syntaxhighlight lang='java'>
jq '.a | .b | .c'
</syntaxhighlight>
=Alternative Operator //=
{{External|https://stedolan.github.io/jq/manual/#Alternativeoperator://}}
=Expressions=
Expressions can be used with the [[Jq_Usage#select.28.29_Filter|select()]] filter.
<Font color=darkgray>Need more semantics here</font>
. >= 2
.id == "blue"
=Built-in Operators and Functions=
{{External|https://stedolan.github.io/jq/manual/#Builtinoperatorsandfunctions}}
==<tt>length</tt>==
The length function displays the length of an array.
<syntaxhighlight lang='bash'>
jq '.menu.popup.menuitem | length' menu.json
</syntaxhighlight>
==<tt>max</tt>, <tt>min</tt>==
The max and min values are used to determine the maximum and minimum values of a field in an array.
==<tt>keys</tt>==
Displays keys of an <font color=darkkhaki>array</font>.
==<tt>has</tt>==
The has function determines whether an array item has a value defined for a given key.
==<tt>map</tt>==
<font color=darkkhaki>TO PROCESS: https://www.linode.com/docs/guides/using-jq-to-process-json-on-the-command-line/ </font>
==<tt>sub</tt>, <tt>gsub</tt> ==
Substitute.
<font size=-1>
sub(regex; tostring)
sub(regex; string; flags)
</font>
Delete the first character:
<syntaxhighlight lang='bash'>
... | jq -r '... | sub("."; "")'
</syntaxhighlight>
==<tt>tonumber</tt>==
==<tt>tostring</tt>==


=Organizatorium=
=Organizatorium=

Latest revision as of 20:55, 25 February 2022

Internal

Pipeline Content

The pipeline flows items that are: primitives, maps and arrays.

Individual strings flowing on a pipeline can be simulated with:

echo \"red\" \"blue\" \"green\" | jq '.'

This is how an array can be injected in the pipeline:

echo "[\"red\", \"blue\"]" | jq '.'

... and this is how a map can be injected in the pipeline:

echo "{\"size\": 10}" | jq '.'

Iteration

An iteration is pipeline content produced by the .[] filter, that converts and array into its individual elements.

Pipe Operator |

The pipe operator '|' combines two filters by feeding the output of the filter specified on the left into the input of the filter specified on the right.

If the left filter produces an iteration, the filter on the right is applied to each of the iteration elements.

Usually the "|" can be omitted on command line and it can be implied:

jq '.a.b.c'

is equivalent with:

jq '.a | .b | .c'

Alternative Operator //

https://stedolan.github.io/jq/manual/#Alternativeoperator://

Expressions

Expressions can be used with the select() filter.

Need more semantics here

. >= 2
.id == "blue"

Built-in Operators and Functions

https://stedolan.github.io/jq/manual/#Builtinoperatorsandfunctions

length

The length function displays the length of an array.

jq '.menu.popup.menuitem | length' menu.json

max, min

The max and min values are used to determine the maximum and minimum values of a field in an array.

keys

Displays keys of an array.

has

The has function determines whether an array item has a value defined for a given key.

map

TO PROCESS: https://www.linode.com/docs/guides/using-jq-to-process-json-on-the-command-line/

sub, gsub

Substitute.

sub(regex; tostring) 
sub(regex; string; flags)

Delete the first character:

... | jq -r '... | sub("."; "")'

tonumber

tostring

Organizatorium

Conversion from a map to an array.

Conversion from array to a map?