Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/content/manual/dev/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,18 @@ sections:
input: '[1, "1"]'
output: ['1', '1']

- title: "`toboolean`"
body: |

The `toboolean` function parses its input as a boolean. It
will convert correctly-formatted strings to their boolean
equivalent, leave booleans alone, and give an error on all other input.

examples:
- program: '.[] | toboolean'
input: '["true", "false", true, false]'
output: ['true', 'false', 'true', 'false']

- title: "`tostring`"
body: |

Expand Down
15 changes: 15 additions & 0 deletions jq.1.prebuilt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,23 @@ static jv f_tonumber(jq_state *jq, jv input) {
return type_error(input, "cannot be parsed as a number");
}

static jv f_toboolean(jq_state *jq, jv input) {
if (jv_get_kind(input) == JV_KIND_TRUE || jv_get_kind(input) == JV_KIND_FALSE) {
return input;
}
if (jv_get_kind(input) == JV_KIND_STRING) {
const char *s = jv_string_value(input);
if (strcmp(s, "true") == 0) {
jv_free(input);
return jv_true();
} else if (strcmp(s, "false") == 0) {
jv_free(input);
return jv_false();
}
}
return type_error(input, "cannot be parsed as a boolean");
}

static jv f_length(jq_state *jq, jv input) {
if (jv_get_kind(input) == JV_KIND_ARRAY) {
return jv_number(jv_array_length(input));
Expand Down Expand Up @@ -1850,6 +1867,7 @@ BINOPS
CFUNC(f_dump, "tojson", 1),
CFUNC(f_json_parse, "fromjson", 1),
CFUNC(f_tonumber, "tonumber", 1),
CFUNC(f_toboolean, "toboolean", 1),
CFUNC(f_tostring, "tostring", 1),
CFUNC(f_keys, "keys", 1),
CFUNC(f_keys_unsorted, "keys_unsorted", 1),
Expand Down
15 changes: 15 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,21 @@ null
4
15

map(toboolean)
["false","true",false,true]
[false,true,false,true]

.[] | try toboolean catch .
[null,0,"tru","truee","fals","falsee",[],{}]
"null (null) cannot be parsed as a boolean"
"number (0) cannot be parsed as a boolean"
"string (\"tru\") cannot be parsed as a boolean"
"string (\"truee\") cannot be parsed as a boolean"
"string (\"fals\") cannot be parsed as a boolean"
"string (\"falsee\") cannot be parsed as a boolean"
"array ([]) cannot be parsed as a boolean"
"object ({}) cannot be parsed as a boolean"

[{"a":42},.object,10,.num,false,true,null,"b",[1,4]] | .[] as $x | [$x == .[]]
{"object": {"a":42}, "num":10.0}
[true, true, false, false, false, false, false, false, false]
Expand Down
7 changes: 7 additions & 0 deletions tests/man.test

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading