gojq is an implementation of the jq command written in Go. You can also embed gojq as a library into your Go product.
Usage
$ echo '{"foo": 128}' | gojq '.foo' 128 $ echo '{"a": {"b": 42}}' | gojq '.a.b' 42 $ echo '{"id": "sample", "10": {"b": 42}}' | gojq '{(.id): .["10"].b}' { "sample": 42 } $ echo '[{"id":1},{"id":2},{"id":3}]' | gojq '.[] | .id' 1 2 3 $ echo '{"a":1,"b":2}' | gojq '.a += 1 | .b *= 2' { "a": 2, "b": 4 } $ echo '{"a":1} [2] 3' | gojq '. as {$a} ?// [$a] ?// $a | $a' 1 2 3 $ echo '{"foo": 4722366482869645213696}' | gojq .foo 4722366482869645213696 # keeps the precision of large numbers $ gojq -n 'def fact($n): if $n < 1 then 1 else $n * fact($n - 1) end; fact(50)' 30414093201713378043612608166064768844377641568960512000000000000 # arbitrary-precision integer calculation
Nice error messages.
$ echo '[1,2,3]' | gojq '.foo & .bar' gojq: invalid query: .foo & .bar .foo & .bar ^ unexpected token "&" $ echo '{"foo": { bar: [] } }' | gojq '.' gojq: invalid json: <stdin> {"foo": { bar: [] } } ^ invalid character 'b' looking for beginning of object key string
Difference from jq
- gojq is fully implemented in Go language and is fully portable. jq depends on the C standard library, so the availability of math functions depends on the library. jq also relies on regular expression libraries, which complicates build scripts.
- gojq implements nice error messages for invalid queries and JSON input. jq’s error message is sometimes difficult to tell where to fix the query.
- gojq does not preserve the order of object keys.Due to this limitation, gojq does not
keys_unsorted
function and--sort-keys
(-S
) option. - gojq supports arbitrary precision integer computations, while jq does not; jq loses the precision of large integers when it comes to computations. Note that even with gojq, all math functions, including floor and round, convert integers to floats; only the addition, subtraction, multiplication, modulo, and division operators (when divisible) preserve integer precision. To compute floor division of integers without losing precision, def idivide($n) can be used. (. – . % $n) / $n;. To round a floating point number to an integer, you can use def ifloor: floor | tostring | tonumber;, but note that this function does not work for large floating point numbers, and also Lose precision for large integers.
- gojq fixes various bugs in jq.
- gojq implementation
@uri
to escape all reserved characters as defined in RFC 3986 section 2.2 ( jq#1506 ), and fix@base64d
to allow binary strings as decoded strings ( jq#1931 ). gojq improves time formatting and parsing; handles %f in strftime and strptime, parses time zone offsets with fromdate and fromdateiso8601, supports time zone names/offsets with %Z/%z in strptime, and uses in daylight saving time %Z formatting to find the correct time zone, gojq supports nanoseconds in date and time functions. - gojq intentionally does not support certain functions; get_jq_origin, get_prog_origin, get_search_list (unstable, not listed in jq documentation), input_line_number, $__loc__ (performance issue), recurse_down (deprecated in jq). gojq does not support some flags; –ascii-output, -a (performance issue), –seq (not commonly used), –sort-keys, -S (default is sort, because map[string]interface{} does not maintain order), –unbuffered (defaults to unbuffered). gojq does not parse the JSON extensions that jq supports; NaN, Infinity and[000]. gojq normalizes floating point numbers to fit double precision floating point numbers. gojq doesn’t support some regex flags (regex engine differences). gojq doesn’t support BOM (encoding/json doesn’t support this). gojq does not allow keywords as function names (declaration def true: .; is a confusing query).
- gojq supports reading YAML input (
--yaml-input
) , which jq does not support. gojq also supports YAML output (--yaml-output
).
#gojq #homepage #documentation #downloads #pure #implementation #News Fast Delivery