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
11 changes: 9 additions & 2 deletions format/json/json.jq
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 3 additions & 0 deletions format/json/testdata/fromjson.fqtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# make sure fromjson decode from value instead of binary for a decode_value
$ fq -n '"\"123\"" | fromjson | fromjson'
123
Loading