Skip to content

Latest commit

 

History

History
29 lines (21 loc) · 843 Bytes

convert_a_number_to_a_string.md

File metadata and controls

29 lines (21 loc) · 843 Bytes

Convert a number to a string

When you are processing data using jq you might run into the problem that some numbers are actually numbers and not strings, so when you want to print them, you get an error.

This is how you convert a number to string for out.

jq -r '.[] | [ .string, .number|tostring ] | join(": ")' <<< '
[{ "number": 9, "string": "nine"},
 { "number": 4, "string": "four"}]
'
nine: 9
four: 4

Or this example:

jq '.[] | .string + ": " + (.number|tostring)' <<< '
[{ "number": 9, "string": "nine"},
 { "number": 4, "string": "four"}]
'

As noted in the resource mentioned below, the parenthesis is important.

Resources and References