jq
jq copied to clipboard
Adds "tobool" builtin function for converting strings to booleans
This PR adds a builtin tobool
function which converts string values to booleans, similar to tonumber
.
This was motivated by the need to create a boolean value based on a value provided through an environment variable. All environment variables are strings, so I needed some mechanism to convert from a string to a boolean. A contrived example of what I wanted (and what this PR implements) is:
$ TEST=true jq --null-input '{"mytest": env.TEST | tobool}'
{
"mytest": true
}
This boolean conversion is already achievable using a function defined in the filter:
$ TEST=true jq --null-input 'def tobool: {"true":true, "false":false}[ . | tostring ]; {"mytest": env.TEST | tobool}'
The inline function definition is rather unwieldily for my use case, making the availability of the builtin function more appealing.