A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
-bash/zsh: jq command not found # Windows (WSL2) sudo apt-get update sudo apt-get install jq # Debian apt-get install jq # Ubuntu apt-get install jq # Alpine apk add jq # Arch Linux pacman -S jq # Kali Linux apt-get install jq # Fedora dnf install jq # OS X brew install jq # Raspbian apt-get install jq # Dockerfile dockerfile.run/jq # Docker docker run cmd.cat/jq jq
jq 是一个轻量级且灵活的命令行 JSON 处理器。jq 命令可以毫不费力地将您拥有的数据格式转换为您想要的格式,并且执行此操作的程序通常比您预期的更短更简单。
jq 可以通过选择、迭代、缩减和以其他方式破坏 JSON 文档等各种方式转换 JSON。例如,运行命令 jq 'map(.price) | add' 将以 JSON 对象数组作为输入并返回其“price”字段的总和。
jq 也可以接受文本输入,但默认情况下,jq 从 stdin 读取 JSON 实体流(包括数字和其他文字)。空格仅用于分隔 1 和 2 以及 true 和 false 等实体。可以指定一个或多个文件,在这种情况下,jq 将从这些文件中读取输入。
jq [options] <jq filter> [file...] jq [options] --args <jq filter> [strings...] jq [options] --jsonargs <jq filter> [JSON_TEXTS...]
Command options: -n, --null-input use `null` as the single input value; -R, --raw-input read each line as string instead of JSON; -s, --slurp read all inputs into an array and use it as the single input value; -c, --compact-output compact instead of pretty-printed output; -r, --raw-output output strings without escapes and quotes; --raw-output0 implies -r and output NUL after each output; -j, --join-output implies -r and output without newline after each output; -a, --ascii-output output strings by only ASCII characters using escape sequences; -S, --sort-keys sort keys of each object on output; -C, --color-output colorize JSON output; -M, --monochrome-output disable colored output; --tab use tabs for indentation; --indent n use n spaces for indentation (max 7 spaces); --unbuffered flush output stream after each output; --stream parse the input value in streaming fashion; --stream-errors implies --stream and report parse error as an array; --seq parse input/output as application/json-seq; -f, --from-file file load filter from the file; -L directory search modules from the directory; --arg name value set $name to the string value; --argjson name value set $name to the JSON value; --slurpfile name file set $name to an array of JSON values read from the file; --rawfile name file set $name to string contents of file; --args consume remaining arguments as positional string values; --jsonargs consume remaining arguments as positional JSON values; -e, --exit-status set exit status code based on the output; -V, --version show the version; --build-configuration show jq's build configuration; -h, --help show the help; -- terminates argument processing; Named arguments are also available as $ARGS.named[], while positional arguments are available as $ARGS.positional[].
jq 执行特定表达式(打印彩色和格式化的 JSON 输出):
cat path/to/file.json | jq '.' ------ $ echo '{"foo": 0}' | jq . { "foo": 0 }
jq 执行特定脚本:
cat path/to/file.json | jq --from-file path/to/script.jq
jq 传递具体参数:
cat path/to/file.json | jq --arg "name1" "value1" --arg "name2" "value2" ... '. + $ARGS.named'
jq 打印特定键:
cat path/to/file.json | jq '.key1, .key2, ...' --------------- # 假设有下面的 JSON 文件:sample.json --------------- { "layoutId": "66f4cc600d05f3006ec20476", "associateDataId": "67186b2b5074105e43d0bba1", "associateObjectName": "合同收款单", "associateMainFieldName": "序号", "associateObjectApiName": "O0000031Object", "associateMainFieldValue": "28", "associateTitleFieldValue": "28" } --------------- cat sample.json | jq '.layoutId' "66f4cc600d05f3006ec20476"
jq 打印特定数组项:
cat path/to/file.json | jq '.[index1], .[index2], ...'
jq 添加/删除特定键:
cat path/to/file.json | jq '. +|- {"key1": "value1", "key2": "value2", ...}' --------------- cat sample.json | jq '. + {"associateMainFieldValue_1":10000}' --------------- # 输出下面 json结果,增加了 associateMainFieldValue_1 --------------- { "layoutId": "66f4cc600d05f3006ec20476", "associateDataId": "67186b2b5074105e43d0bba1", "associateObjectName": "合同收款单", "associateMainFieldName": "序号", "associateObjectApiName": "O0000031Object", "associateMainFieldValue": "28", "associateTitleFieldValue": "28", "associateMainFieldValue_1": 10000 }