Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq: find and replace null with zero (0)

Tags:

json

null

jq

Given this output:

cat file.json | jq -cr '.[] | .name, .time.total, .dueOn'

I get the output:

Task one
35160
2020-08-14
Task two
null
null
Task three
null
null
Task 4
2280
null

The time.total value of "null" I'd like to change to 0, so I can perform math on it. (I intend to divide by 3600 to covert the value to hours, e.g 1.25)

I've read countless SE articles, and no doubt my jq chops aren't where they need to be. But I hope there's a basic solution for this!

Desired output would be:

Task one
35160
2020-08-14
Task two
0
null
Task three
0
null
Task 4
2280
null
like image 642
Creative Arc Avatar asked Sep 17 '25 05:09

Creative Arc


1 Answers

Instead of .time.total, you could write:

(.time.total // 0)
like image 158
peak Avatar answered Sep 19 '25 21:09

peak