Skip to content

Commit 5b1d9ab

Browse files
authored
Merge pull request #1269 from wader/fromjson-decode_value-string
json: Make fromjson less confusing when used with a decode_value
2 parents 67cae41 + 12ba0d6 commit 5b1d9ab

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

format/json/json.jq

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
def tojson($opts): _to_json($opts);
33
def tojson: _to_json(null);
44
# overrides jq's standard fromjson
5-
# NOTE: should be kept in sync with format_decode.jq
6-
def fromjson: decode("json") | if ._error then error(._error.error) end;
5+
# NOTE:
6+
# should be kept in sync with format_decode.jq and can't use from_json as
7+
# it's not defined yet.
8+
# also uses tovalue on it's input to care of the where the input is a decode_value
9+
# string which without would end up decoding the "backing" binary instead.
10+
# Ex:
11+
# $ fq -n '"\"1,2,3\"" | fromjson | tobytes | tostring'
12+
# "\"1,2,3\""
13+
def fromjson: tovalue | decode("json") | if ._error then error(._error.error) end;
714

815
def _json__todisplay: tovalue;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# make sure fromjson decode from value instead of binary for a decode_value
2+
$ fq -n '"\"123\"" | fromjson | fromjson'
3+
123

0 commit comments

Comments
 (0)