0x1
在编写 Shell 脚本时,如果需要解析 json 数据时,可以使用第三方工具库 jq 来解析
官网地址:https://stedolan.github.io/jq/
0x2 安装
- Debian and Ubuntu repositories. Install using
sudo apt-get install jq - Fedora repository. Install using
sudo dnf install jq - openSUSE repository. Install using
sudo zypper install jq - Arch repository. Install using
sudo pacman -S jq - Homebrew to install with
brew install jq - MacPorts to install with
port install jq - …更多请查看官方文档
0x3 使用
假如有一个文件 data.json ,内容如下:
{
"name": "greycode",
"version": "1.0.0"
}
可以使用如下命令解析:
cat data.json | jq -r '.'
获取指定 key:
cat data.json | jq -r '. | .name'