
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許可證獲得。