
JSON流编辑器
JJ是一个命令行实用程序,提供了一种快速,简单的方法来检索或更新JSON文档的值。它由Gjson和Sjson提供支持。
之所以快,是因为它避免了分析JSON无关的部分,跳过不适用的值,并在找到或更新目标值后立即中止。
brew install tidwall/jj/jj
make
或下载用于Linux,OSX,Windows或FreeBSD的预构建的二进制文件。
$ jj -h
usage: jj [-v value] [-purOD] [-i infile] [-o outfile] keypath
examples: jj keypath read value from stdin
or: jj -i infile keypath read value from infile
or: jj -v value keypath edit value
or: jj -v value -o outfile keypath edit value and write to outfile
options:
-v value Edit JSON key path value
-p Make json pretty, keypath is optional
-u Make json ugly, keypath is optional
-r Use raw values, otherwise types are auto-detected
-n Do not output color or extra formatting
-O Performance boost for value updates
-D Delete the value at the specified key path
-l Output array values on multiple lines
-i infile Use input file instead of stdin
-o outfile Use output file instead of stdout
keypath JSON key path (like "name.last")
JJ使用路径语法来查找值。
得到一个字符串:
$ echo ' {"name":{"first":"Tom","last":"Smith"}} ' | jj name.last
Smith得到JSON的街区:
$ echo ' {"name":{"first":"Tom","last":"Smith"}} ' | jj name
{ " first " : " Tom " , " last " : " Smith " }尝试获取不存在的密钥:
$ echo ' {"name":{"first":"Tom","last":"Smith"}} ' | jj name.middle
null获取原始字符串值:
$ echo ' {"name":{"first":"Tom","last":"Smith"}} ' | jj -r name.last
" Smith "通过索引获取数组值:
$ echo ' {"friends":["Tom","Jane","Carol"]} ' | jj friends.1
Jane使用..路径前缀来支持JSON线。在指定时,将多线文档视为数组。
例如:
{"name": "Gilbert", "age": 61}
{"name": "Alexa", "age": 34}
{"name": "May", "age": 57}
..# >> 4
..1 >> {"name": "Alexa", "age": 34}
..#.name >> ["Gilbert","Alexa","May"]
..#[name="May"].age >> 57
用于设置值的路径语法比获得值的差异很小。
-v value选项自动检测为数字,布尔值,null或字符串。您可以通过包括-r选项来覆盖自动检测和输入RAW JSON。这对于原始JSON块(例如对象,数组或预上板上的字符串)很有用。
更新一个值:
$ echo ' {"name":{"first":"Tom","last":"Smith"}} ' | jj -v Andy name.first
{ " name " :{ " first " : " Andy " , " last " : " Smith " }}设置一个新值:
$ echo ' {"name":{"first":"Tom","last":"Smith"}} ' | jj -v 46 age
{ " age " :46, " name " :{ " first " : " Tom " , " last " : " Smith " }}设置一个新的嵌套值:
$ echo ' {"name":{"first":"Tom","last":"Smith"}} ' | jj -v relax task.today
{ " task " :{ " today " : " relax " }, " name " :{ " first " : " Tom " , " last " : " Smith " }}用索引替换数组值:
$ echo ' {"friends":["Tom","Jane","Carol"]} ' | jj -v Andy friends.1
{ " friends " :[ " Tom " , " Andy " , " Carol " ]}附加数组:
$ echo ' {"friends":["Tom","Jane","Carol"]} ' | jj -v Andy friends.-1
{ " friends " :[ " Tom " , " Andy " , " Carol " , " Andy " ]}设置一个超过范围的数组值:
$ echo ' {"friends":["Tom","Jane","Carol"]} ' | jj -v Andy friends.5
{ " friends " :[ " Tom " , " Andy " , " Carol " ,null,null, " Andy " ]}设置JSON的原始块:
$ echo ' {"name":"Carol"} ' | jj -r -v ' ["Tom","Andy"] ' friends
{ " friends " :[ " Tom " , " Andy " ], " name " : " Carol " }启动新的JSON文档:
$ echo ' ' | jj -v ' Sam ' name.first
{ " name " :{ " first " : " Sam " }}删除一个值:
$ echo ' {"age":46,"name":{"first":"Tom","last":"Smith"}} ' | jj -D age
{ " name " :{ " first " : " Tom " , " last " : " Smith " }}按索引删除数组值:
$ echo ' {"friends":["Andy","Carol"]} ' | jj -D friends.0
{ " friends " :[ " Carol " ]}删除数组中的最后一项:
$ echo ' {"friends":["Andy","Carol"]} ' | jj -D friends.-1
{ " friends " :[ " Andy " ]}当呼叫者期望已经存在指定键盘上的值时,可以使用-O选项。
使用此选项可以将操作加快多达6倍,但在不存在该值时放慢多达20%。
例如:
echo '{"name":{"first":"Tom","last":"Smith"}}' | jj -v Tim -O name.first
-O告诉jj,该name.first可能存在,因此首先尝试FastTrack操作。
-p标志将使输出json漂亮。
$ echo '{"name":{"first":"Tom","last":"Smith"}}' | jj -p name
{
"first": "Tom",
"last": "Smith"
}
当指定-p标志时,键盘是可选的,可以使整个JSON文档变得漂亮。
$ echo '{"name":{"first":"Tom","last":"Smith"}}' | jj -p
{
"name": {
"first": "Tom",
"last": "Smith"
}
}
-u标志将通过挤压新线和空格将JSON压缩为最少的字符。
JJ与JQ的快速比较。测试JSON文件是旧金山206,560个城市包裹的180MB文件。
在2015年MacBook Pro运行JQ 1.5和JJ 1.0.0上测试
JQ:
$ time cat citylots.json | jq -cM .features[10000].properties.LOT_NUM
" 091 "
real 0m5.486s
user 0m4.870s
sys 0m0.686sJJ:
$ time cat citylots.json | jj -r features.10000.properties.LOT_NUM
" 091 "
real 0m0.354s
user 0m0.161s
sys 0m0.321sJQ:
$ time cat citylots.json | jq -cM ' .features[10000].properties.LOT_NUM="12A" ' > /dev/null
real 0m13.579s
user 0m16.484s
sys 0m1.310sJJ:
$ time cat citylots.json | jj -O -v 12A features.10000.properties.LOT_NUM > /dev/null
real 0m0.431s
user 0m0.201s
sys 0m0.295s乔什·贝克@Tidwall
JJ源代码可根据MIT许可证获得。