Filter empty and/or null values with jq Filter empty and/or null values with jq json json

Filter empty and/or null values with jq


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.


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} + .