惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Y
Y Combinator Blog
T
Tailwind CSS Blog
G
Google Developers Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 聂微东
爱范儿
爱范儿
博客园 - 【当耐特】
腾讯CDC
T
The Blog of Author Tim Ferriss
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
C
Check Point Blog
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
博客园_首页
Stack Overflow Blog
Stack Overflow Blog
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟
命令行神器之jq
2015-10-29 · via 夜行人

神器有很多种,有一种是你一旦试用,就无法想象遇到它之前都是怎么过来的

jq属于这种

#是什么

jq is a lightweight and flexible command-line JSON processor

jq是一款命令行工具,专门用来处理json文件,与所有优秀的命令行工具一样,短小精悍,通过管道他们相互协作,组合出惊人的效果

jq的使用习惯和sed/grep相似

#安装

  • mac: brew install jq
  • ubuntu: sudo apt-get install jq

#使用 cat account.json

1
{"username": "wwj", "bio": null, "requires_parental_consent": true, "name": "wwj", "country": null, "is_active": true, "profile_image": {"image_url_full": "http://example.com/static/images/default-theme/default-profile_500.de2c6854f1eb.png", "image_url_large": "http://example.com/static/images/default-theme/default-profile_120.33ad4f755071.png", "image_url_medium": "http://example.com/static/images/default-theme/default-profile_50.5fb006f96a15.png", "image_url_small": "http://example.com/static/images/default-theme/default-profile_30.ae6a9ca9b390.png", "has_image": false}, "year_of_birth": null, "level_of_education": null, "goals": null, "language_proficiencies": [], "gender": null, "mailing_address": null, "email": "wwj@example.com", "date_joined": "2015-05-13T09:42:45Z"}

###原文输出(格式化json) cat ./account.json|jq '.'

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
  "username": "wwj",
  "bio": null,
  "requires_parental_consent": true,
  "name": "wwj",
  "country": null,
  "is_active": true,
  "profile_image": {
    "image_url_full": "http://example.com/static/images/default-theme/default-profile_500.de2c6854f1eb.png",
    "image_url_large": "http://example.com/static/images/default-theme/default-profile_120.33ad4f755071.png",
    "image_url_medium": "http://example.com/static/images/default-theme/default-profile_50.5fb006f96a15.png",
    "image_url_small": "http://example.com/static/images/default-theme/default-profile_30.ae6a9ca9b390.png",
    "has_image": false
  },
  "year_of_birth": null,
  "level_of_education": null,
  "goals": null,
  "language_proficiencies": [],
  "gender": null,
  "mailing_address": null,
  "email": "wwj@example.com",
  "date_joined": "2015-05-13T09:42:45Z"
}

###过滤 cat ./account.json|jq '.profile_image'

1
2
3
4
5
6
7
{
  "image_url_full": "http://example.com/static/images/default-theme/default-profile_500.de2c6854f1eb.png",
  "image_url_large": "http://example.com/static/images/default-theme/default-profile_120.33ad4f755071.png",
  "image_url_medium": "http://example.com/static/images/default-theme/default-profile_50.5fb006f96a15.png",
  "image_url_small": "http://example.com/static/images/default-theme/default-profile_30.ae6a9ca9b390.png",
  "has_image": false
}

如果key不存在则返回null

cat ./account.json|jq '.testname' 返回null

###支持嵌套 cat ./account.json|jq '.profile_image.image_url_full'

"http://example.com/static/images/default-theme/default-profile_500.de2c6854f1eb.png"

###语法检查 cat test.json

1
2
3
4
5
{
    "a":"a",
    ddd"b":"b",
    "c":"c"
}

cat test.json | jq '.'返回parse error: Invalid numeric literal at line 3, column 8

##在线体验 jqplay.org

##上手 Tutorial

##文档 manual

##进阶 Cookbook

##在edx-platform中使用 /edx/app/edxapp/*.*.json用于覆盖aws.py和devstack.py里的配置参数,为用户配置edx提供了便利,但是由于缺乏语法检查一个小失误(诸如漏了逗号,漏了引号),就会造成edx启动失败,而且错误信息几乎无法定位。

有了jq,可以方便地检查语法以及查看参数

如果我们把jq当做处理json数据的sed,还可以方便用来写配置edx的脚本

###我的实践

文章作者 种瓜

上次更新 2015-10-29