← 返回命令列表

Linux command

gojq 命令

文件

涉及管道、覆盖或删除,执行前请先确认路径和参数。

常用示例

Pretty print JSON

cat [file.json] | gojq .

Extract a field

cat [file.json] | gojq '.[field]'

Filter array elements

cat [file.json] | gojq '.[] | select(.active == true)'

Map and transform

cat [file.json] | gojq '[.[] | {name: .title, id: .uuid}]'

Read from file directly

gojq '.users[]' [file.json]

Process YAML input

gojq --yaml-input '.spec.containers[]' [deployment.yaml]

Output as YAML

gojq --yaml-output '.data' [file.json]

Use variables

gojq --arg name "[value]" '.items[] | select(.name == $name)' [file.json]

说明

gojq is a pure Go implementation of jq, the JSON processor. It provides the same query language for filtering, transforming, and extracting data from JSON, with additional native YAML support. The filter language uses | for pipelines, . for field access, [] for iteration, and select() for filtering. Complex transformations combine these with object construction, array slicing, and built-in functions. YAML input and output make gojq particularly useful for Kubernetes and other YAML-heavy workflows. Variables with --arg enable parameterized queries for scripting.

参数

-r, --raw-output
Output strings without JSON quotes.
-c, --compact-output
Compact output instead of pretty-printed.
-n, --null-input
Don't read input; useful with --argjson.
-e, --exit-status
Set exit code based on output.
-s, --slurp
Read all inputs into array.
-S, --sort-keys
Sort object keys in output.
-C, --color-output
Force colored output.
-M, --monochrome-output
Disable colored output.
--tab
Use tabs for indentation.
--indent _n_
Set indentation level.
--yaml-input
Parse input as YAML.
--yaml-output
Output as YAML instead of JSON.
--arg _name_ _value_
Set variable to string value.
--argjson _name_ _json_
Set variable to JSON value.
--slurpfile _name_ _file_
Set variable to file contents as array.
--rawfile _name_ _file_
Set variable to raw file contents.
-f, --from-file _file_
Read filter from file.
-L _path_
Add directory to module search path.

FAQ

What is the gojq command used for?

gojq is a pure Go implementation of jq, the JSON processor. It provides the same query language for filtering, transforming, and extracting data from JSON, with additional native YAML support. The filter language uses | for pipelines, . for field access, [] for iteration, and select() for filtering. Complex transformations combine these with object construction, array slicing, and built-in functions. YAML input and output make gojq particularly useful for Kubernetes and other YAML-heavy workflows. Variables with --arg enable parameterized queries for scripting.

How do I run a basic gojq example?

Run `cat [file.json] | gojq .` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -r, --raw-output do in gojq?

Output strings without JSON quotes.