diff --git a/format/json/json.jq b/format/json/json.jq index fdc29ebde..d9682a237 100644 --- a/format/json/json.jq +++ b/format/json/json.jq @@ -2,7 +2,14 @@ def tojson($opts): _to_json($opts); def tojson: _to_json(null); # overrides jq's standard fromjson -# NOTE: should be kept in sync with format_decode.jq -def fromjson: decode("json") | if ._error then error(._error.error) end; +# NOTE: +# should be kept in sync with format_decode.jq and can't use from_json as +# it's not defined yet. +# also uses tovalue on it's input to care of the where the input is a decode_value +# string which without would end up decoding the "backing" binary instead. +# Ex: +# $ fq -n '"\"1,2,3\"" | fromjson | tobytes | tostring' +# "\"1,2,3\"" +def fromjson: tovalue | decode("json") | if ._error then error(._error.error) end; def _json__todisplay: tovalue; diff --git a/format/json/testdata/fromjson.fqtest b/format/json/testdata/fromjson.fqtest new file mode 100644 index 000000000..b68602fee --- /dev/null +++ b/format/json/testdata/fromjson.fqtest @@ -0,0 +1,3 @@ +# make sure fromjson decode from value instead of binary for a decode_value +$ fq -n '"\"123\"" | fromjson | fromjson' +123