jq icon indicating copy to clipboard operation
jq copied to clipboard

Adds "tobool" builtin function for converting strings to booleans

Open brahmlower opened this issue 4 years ago • 1 comments

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.

brahmlower avatar Apr 10 '20 11:04 brahmlower

Coverage Status

Coverage increased (+0.02%) to 84.149% when pulling 507453766537d3eb00b16a770255888efb83c672 on bplower:tobool-builtin into ccc79e592cfe1172db5f2def5a24c2f7cfd418bf on stedolan:master.

coveralls avatar Apr 10 '20 11:04 coveralls