Jq has become the de-facto json console tool. Apart from slicing and dicing your jsons, one can do things like assigning new fields to it, or modifying existing ones.
As I explain in my scripting field guide, changing epoch times to a readable timestamp can be done like:
echo '{"date":1643192480}' | jq '.date|=todateiso8601' # {"date": "2022-01-26T10:22:48Z"}
To assign multiple fields in jq, you must interleave '|' inbetween, effectively piping the result to the next assignment.
And here's the bash snippet of the day:
join_by() { local IFS="$1"; shift; echo "$*"; }
pretty_dates() {
jq "$(join_by "|" ${@/%/|=todateiso8601})"
}
`curl http://..... | pretty_dates .start_date .end_date` will build the proper assignments and use jq so that we get a nicely formatted json output.
Again, for small helpers like these, a bit of advanced bash can get you very very far.
The explanation of the weird line is: from the default (${@}
) params as an array, append every element (/%/
) with |=todateiso8601
). That, joined by |
. And that becomes the "filter" to jq.