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

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的脚本

###我的实践