I am validating a json file, I need to validate in a field that if it is null, assign a default value, I can’t find how to perform a validation with if conditional
file.json
{
"timeout": 100
}
jq -r .timeout fiile.json
here it prints the value correctly, I need to validate if this field is null to assign it a default value with jq
Thanks in advance
2
Answers
Use the update operator
|=
to update the field in question. For the conditional just compare tonull
.If your input file is
it will stay the same, as
.timeout
is notnull
. But if it isthen it will be changed to the default value given, here:
Note that this only triggers for the content of
null
. There are other means to test forfalse
, the number zero0
, the empty string""
, etc., even the complete absence of that field.You can try alternative operator
//