Creative Arc
Creative Arc

Reputation: 53

jq: find and replace null with zero (0)

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

Upvotes: 4

Views: 2984

Answers (1)

peak
peak

Reputation: 116750

Instead of .time.total, you could write:

(.time.total // 0)

Upvotes: 10

Related Questions