[Bash] Read and Use JSON in Bash with jq

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'
posted @   Zhentiw  阅读(56)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-02-11 [HTML5] Add an SVG Image to a Webpage and Get a Reference to the Internal Elements in JavaScript
2019-02-11 [Angular] Angular i18n Alternative Expressions Support (select)
2019-02-11 [React + Functional Programming ADT] Create Redux Middleware to Dispatch Multiple Actions
2019-02-11 [React + Functional Programming ADT] Connect State ADT Based Redux Actions to a React Application
2019-02-11 [Functional Programming ADT] Create a Redux Store for Use with a State ADT Based Reducer
2019-02-11 [CSS3] Make a One-time CSS Animation that Does Not Revert to its Original Style
2017-02-11 [Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge
点击右上角即可分享
微信分享提示