Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 537

In JQ how to safely process optionally null values

$
0
0

I have this JSON:

[  {"alert_timestamp": "2024-10-23T08:49:08Z","odin_timestamp": "2024-10-23T08:50:10Z","alert_url": null,"comments": null,"owner": null,"updated_date": null,"clear_timestamp": "2024-10-23T09:27:39Z","outage_duration_in_minutes": 38.52,"published_on": "DEFAULT","atr_exec_id": null,"atr_status": null  }]

And, in the even that some fields, like comments isn't null, I need to strip out newlines and tabs. If it is null, then keep it null

I have tried the following:

map(.comments? |= (gsub("\r\n"; "") | gsub("\t"; ""))) |map(.alert_message? |= (gsub("\r\n"; "") | gsub("\t"; "")))

Which results in:

jq: error (at 16.json:338): null (null) cannot be matched, as it is not a string

And I have tried the // alternative operator:

map((.comments // "") |= (gsub("\r\n"; "") | gsub("\t"; ""))) |map((.alert_message // "") |= (gsub("\r\n"; "") | gsub("\t"; "")))

yielding:

jq: error (at 16.json:338): Invalid path expression with result ""

There has to be a simple way to handle this without an if else


Viewing all articles
Browse latest Browse all 537

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>