Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq - How to filter a json that does not contain

Tags:

json

jq

I have an aws query that I want to filter in jq. I want to filter all the imageTags that don't end with "latest"

So far I did this but it filters things containing "latest" while I want to filter things not containing "latest" (or not ending with "latest")

aws ecr describe-images --repository-name <repo> --output json | jq '.[]' | jq '.[]' | jq "select ((.imagePushedAt < 14893094695) and (.imageTags[] | contains(\"latest\")))" 

Thanks

like image 220
Gavriel Fishel Avatar asked Mar 12 '17 11:03

Gavriel Fishel


People also ask

What is JSON filter?

The json filter converts a JavaScript object into a JSON string. This filter can be useful when debugging your applications. The JavaScript object can be any kind of JavaScript object.

What is 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.

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.

What is jq slurp?

The slurp option ( -s ) changes the input to the jq program. It reads all the input values and build an array for the query input. Using with the raw input option ( -R ) means reading the entire input as a string. The inputs function is a special stream that emits the remaining JSON values given to the jq program.


1 Answers

You can use not to reverse the logic

(.imageTags[] | contains(\"latest\") | not) 

Also, I'd imagine you can simplify your pipeline into a single jq call.

like image 107
J. Doe Avatar answered Sep 24 '22 04:09

J. Doe