Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select items in JQ based on value in array

Tags:

I have a file with lines like this:

{"items":["blue","green"]} {"items":["yellow","green"]} {"items":["blue","pink"]} 

How can I use jq to select and show only the JSON values that have "blue" in their "items" array?

So the output would be:

{"items":["blue","green"]} {"items":["blue","pink"]} 
like image 477
K2xL Avatar asked Sep 03 '14 17:09

K2xL


People also ask

What is a jq filter?

A jq program is a "filter": it takes an input, and produces an output. There are a lot of builtin filters for extracting a particular field of an object, or converting a number to a string, or various other standard tasks.

How does jq filter data?

Identity: . (dot) in jq represent the entire input without any filters. JQ command followed by a dot will output the input as it is with some formatting to make it pretty. You can use jq . command to format the output of a curl command.

Does jq use JSONPath?

JSONPath distinguishes between the "root object or element" ($) and "the current object or element" (.). jq simply uses . to refer to the current JSON entity and so it is context-dependent: it can refer to items in the input stream of the jq process as a whole, or to the output of a filter.


2 Answers

Found out the answer

jq 'select(.items | index("blue"))' 
like image 118
K2xL Avatar answered Oct 08 '22 20:10

K2xL


On Jan 30, 2017, a builtin named IN was added for efficiently testing whether a JSON entity is contained in a stream. It can also be used for efficiently testing membership in an array. In the present case, the relevant usage would be:

select( .items as $items | "blue" | IN($items[]) ) 

If your jq does not have IN/1, then so long as your jq has first/1, you can use this equivalent definition:

def IN(s): . as $in | first(if (s == $in) then true else empty end) // false; 

any/0

Using any/0 here is relatively inefficient, e.g. compared to using any/1:

select( any( .items[]; . == "blue" )) 

(In practice, index/1 is usually fast enough, but its implementation currently (jq 1.5 and versions through at least July 2017) is suboptimal.)

like image 43
peak Avatar answered Oct 08 '22 19:10

peak