Filter empty and/or null values with jq
Asked Answered
S

5

29

I have a file with jsonlines and would like to find empty values.

{"name": "Color TV", "price": "1200", "available": ""}
{"name": "DVD player", "price": "200", "color": null}

And would like to output empty and/or null values and their keys:

available: ""
color: null

I think it should be something like cat myexample | jq '. | select(. == "")', but is not working.

Spinelli answered 20/6, 2019 at 18:37 Comment(0)
A
20

The tricky part here is emitting the keys without quotation marks in a way that the empty string is shown with quotation marks. Here is one solution that works with jq's -r command-line option:

to_entries[]
| select(.value | . == null or . == "")
| if .value == "" then .value |= "\"\(.)\"" else . end
| "\(.key): \(.value)"

Once the given input has been modified in the obvious way to make it valid JSON, the output is exactly as specified.

Anhydrite answered 20/6, 2019 at 22:6 Comment(4)
Thanks! Corrected the input. I don't really understand the logic here. Can't we just filter keys with empty value? Why convert to_entries and then select entries and then... don't really know what happens there!Whereas
You might like to read the jq tutorial and the relevant sections of the jq manual -- see jqAnhydrite
What these to_entries[] mean and how do you use this with jq ?Deandeana
@Deandeana - Please see the jq manual stedolan.github.io/jq/manualAnhydrite
U
17

Take a look at this snippet https://blog.nem.ec/code-snippets/jq-ignore-nulls/

jq -r '.firstName | select( . != null )' file.json
Unusual answered 25/1, 2023 at 14:2 Comment(1)
jq -r '.firstName | select(.)' file.jsonOfficiate
A
14

Some people may find the following jq program more useful for identifying keys with null or empty string values:

with_entries(select(.value |.==null or . == ""))

With the sample input, this program would produce:

{"available":""}
{"color":null}

Adding further information, such as the input line or object number, would also make sense, e.g. perhaps:

with_entries(select(.value |.==null or . == ""))
| select(length>0)
| {n: input_line_number} + .
Anhydrite answered 3/5, 2021 at 8:57 Comment(0)
S
3

With a single with_entries(if .value == null or .value == "" then empty else . end) filter expression it's possible to filter out null and empty ("") values.

Without filtering:

echo '{"foo": null, "bar": ""}' | jq '.'
{
  "foo": null,
  "bar": ""
}

With filtering:

echo '{"foo": null, "bar": ""}' |  jq 'with_entries(if .value == null or .value == "" then empty else . end)'
{}
Sardinian answered 23/9, 2022 at 12:33 Comment(0)
F
1

You can use select(.) to filter out the nulls

Fontainebleau answered 7/9, 2023 at 14:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.