I have a json array of objects in which I would like to create some string using some values. What I tried was:
cat a.json | jq 'map(.id+" "+tostring(.time))'
It gives the error:
jq: error: tostring/1 is not defined at <top-level>, line 1:
If I omit the tostring, then the problem is of course that time
is not a string but a number.
Is there a way to do this? I could do a workaround using sed+tr, but it is inelegant.
EDIT: It was suggested to include some sample input. Behold!
[{
"id": "399",
"time": 1705655705
},
{
"id": "409",
"time": 1723587399
}]
There's only a tostring/0
function. Try (.time | tostring)
instead.
jq 'map(.id + " " + (.time | tostring))' a.json
Alternatively, use string interpolation, which automatically converts to string:
jq 'map("\(.id) \(.time)")' a.json