Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jq: jq: error: tostring/1 is not defined at <top-level>, line 1:

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a way to do this? I could do a workaround using sed+tr, but it is inelegant.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading