Skip to content

Commit c350566

Browse files
authored
Adds "toboolean" builtin function for converting strings to booleans (#2098)
1 parent 92daac9 commit c350566

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

docs/content/manual/dev/manual.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,18 @@ sections:
14771477
input: '[1, "1"]'
14781478
output: ['1', '1']
14791479

1480+
- title: "`toboolean`"
1481+
body: |
1482+
1483+
The `toboolean` function parses its input as a boolean. It
1484+
will convert correctly-formatted strings to their boolean
1485+
equivalent, leave booleans alone, and give an error on all other input.
1486+
1487+
examples:
1488+
- program: '.[] | toboolean'
1489+
input: '["true", "false", true, false]'
1490+
output: ['true', 'false', 'true', 'false']
1491+
14801492
- title: "`tostring`"
14811493
body: |
14821494

jq.1.prebuilt

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/builtin.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,23 @@ static jv f_tonumber(jq_state *jq, jv input) {
463463
return type_error(input, "cannot be parsed as a number");
464464
}
465465

466+
static jv f_toboolean(jq_state *jq, jv input) {
467+
if (jv_get_kind(input) == JV_KIND_TRUE || jv_get_kind(input) == JV_KIND_FALSE) {
468+
return input;
469+
}
470+
if (jv_get_kind(input) == JV_KIND_STRING) {
471+
const char *s = jv_string_value(input);
472+
if (strcmp(s, "true") == 0) {
473+
jv_free(input);
474+
return jv_true();
475+
} else if (strcmp(s, "false") == 0) {
476+
jv_free(input);
477+
return jv_false();
478+
}
479+
}
480+
return type_error(input, "cannot be parsed as a boolean");
481+
}
482+
466483
static jv f_length(jq_state *jq, jv input) {
467484
if (jv_get_kind(input) == JV_KIND_ARRAY) {
468485
return jv_number(jv_array_length(input));
@@ -1886,6 +1903,7 @@ BINOPS
18861903
CFUNC(f_dump, "tojson", 1),
18871904
CFUNC(f_json_parse, "fromjson", 1),
18881905
CFUNC(f_tonumber, "tonumber", 1),
1906+
CFUNC(f_toboolean, "toboolean", 1),
18891907
CFUNC(f_tostring, "tostring", 1),
18901908
CFUNC(f_keys, "keys", 1),
18911909
CFUNC(f_keys_unsorted, "keys_unsorted", 1),

tests/jq.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,21 @@ null
656656
4
657657
15
658658

659+
map(toboolean)
660+
["false","true",false,true]
661+
[false,true,false,true]
662+
663+
.[] | try toboolean catch .
664+
[null,0,"tru","truee","fals","falsee",[],{}]
665+
"null (null) cannot be parsed as a boolean"
666+
"number (0) cannot be parsed as a boolean"
667+
"string (\"tru\") cannot be parsed as a boolean"
668+
"string (\"truee\") cannot be parsed as a boolean"
669+
"string (\"fals\") cannot be parsed as a boolean"
670+
"string (\"falsee\") cannot be parsed as a boolean"
671+
"array ([]) cannot be parsed as a boolean"
672+
"object ({}) cannot be parsed as a boolean"
673+
659674
[{"a":42},.object,10,.num,false,true,null,"b",[1,4]] | .[] as $x | [$x == .[]]
660675
{"object": {"a":42}, "num":10.0}
661676
[true, true, false, false, false, false, false, false, false]

tests/man.test

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)