码迷,mamicode.com
首页 > Web开发 > 详细

[Bash] Read and Use JSON in Bash with jq

时间:2021-02-15 12:41:34      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:cti   ems   ref   osi   http   tun   get   eal   too   

jq

Bash, unfortunately, doesn’t ship with a command that can work with JSON natively. In this lesson, we’ll learn how to read and do basic queries on JSON with jq, an installable command line tool that makes working with JSON in bash really easy. We‘ll pipe the JSON output of the Github API to jq to extract values.

Note: jq has to be installed. On macOS, the easiest way to do this is to run brew install jq. To view install instructions for all platforms, see https://stedolan.github.io/jq/download/.

Install

brew install jq

Usage

echo ‘{"foo": 123}‘ | jq ‘.foo‘ ## access foo‘s value, print 123
echo ‘{"a": {"b": 123}}‘ | jq ‘.a.b‘ ## 123

Example

curl -s https://api.github.com/repos/facebook/react | jq ‘.stargazers_count‘

Array

echo ‘[1,2,3]‘ | jq ‘.[]‘
## 1
## 2
## 3
echo ‘[{"id": 1}, {"id": 2}]‘ | jq ‘.[].id‘
## 1
## 2

Example

curl -s https://api.github.com/search/repositories?q=service+worker | jq ‘.items[].name‘

[Bash] Read and Use JSON in Bash with jq

标签:cti   ems   ref   osi   http   tun   get   eal   too   

原文地址:https://www.cnblogs.com/Answer1215/p/14398214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!