神器有很多种,有一种是你一旦试用,就无法想象遇到它之前都是怎么过来的
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
|
|
###原文输出(格式化json)
cat ./account.json|jq '.'
|
|
###过滤
cat ./account.json|jq '.profile_image'
|
|
如果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
|
|
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