I'm attempting to generate json output where the input is coming from shell variables.
happystring="Bob Ross"
unhappynumber1="1942"
unhappyboolean=true
JSON=$(jq -n \
--arg happystring "$happystring" --arg unhappynumber1 "$unhappynumber1" \
--arg unhappyboolean $unhappyboolean \
'
{
happystring: $happystring,
unhappynumber1: $unhappynumber1,
unhappyboolean: $unhappyboolean
}
')
echo "$JSON" | jq
Produces this output:
{
"happystring": "Bob Ross",
"unhappynumber1": "1942",
"unhappyboolean": "true"
}
I know I can use tonumber
to convert a string to a number in a simple filter. However, I cannot figure out how to convert a string to a boolean. And I'm having trouble reasoning how to do either when sourcing from shell vars and creating new json as output.
Desired output:
{
"happystring": "Bob Ross",
"unhappynumber1": 1942,
"unhappyboolean": true
}
Would it be easier or more clear if I produced the json and stored it in a shell var in one step, and then performed the additional conversion in a second step?