这是一个过滤器,它发出所有路径的概要流
输入实体中的长度
路径 [p1, p2, ...] 的概要是通过替换来构造的
带有“.[]”的整数组件,以及带有“.”前缀的字符串组件,
例如,如果 i 和 j 是整数,那么
[i, "keyname", j] 将表示为 .[].keyname.[]
这是使用jq -r 生成的输出示例:
.[]
.[].data
.[].data.children
.[].data.modhash
.[].kind
paths_synopsis/1
# If depth<0 then select paths of length equal to -depth
def paths_synopsis(depth):
[ paths
| if depth > 0 then select(length <= depth)
elif (depth < 0) then select(length == -depth)
else . end
| [.[]|if type=="number" then "[]" else . end]]
| unique
| .[]
| "." + join(".")
;
非常大的 JSON 实体
jq 有一个流式解析器,用于处理非常大的 JSON 实体。
以下过滤器旨在与 jq 流解析器 (jq --stream) 一起使用
在管道中,其中的第二个组件使概要独一无二,如本例所示:
jq --arg depth 0 -c --stream -f synopsis.jq input.json | sort -u
在以下公式中,必须在命令行上指定所需的深度限制。
指定 0 表示无限制。
概要.jq
# Usage: jq --arg depth DEPTH -c --stream -f synopsis.jq input.json | sort -u
# or: jq --arg depth DEPTH -c --stream -f synopsis.jq input.json | jq -s -c unique[]
def synopsis(depth):
select(length == 2)
| .[0]
| if depth > 0 then select(length <= depth)
elif (depth < 0) then select(length == -depth)
else . end
| map( if type=="number" then [] else . end) ;
synopsis( $depth | if . then tonumber else 0 end )
示例:
curl -Ss 'http://forecast.weather.gov/MapClick.php?FcstType=json&lat=39.56&lon=-104.85' |
jq --arg depth 0 -c --stream -f synopsis.jq |
sort -u | head -n 50
["creationDate"]
["creationDateLocal"]
["credit"]
["currentobservation","Altimeter"]
["currentobservation","Date"]
["currentobservation","Dewp"]
["currentobservation","Gust"]
["currentobservation","Relh"]
["currentobservation","SLP"]
["currentobservation","Temp"]
["currentobservation","Visibility"]
["currentobservation","Weather"]
["currentobservation","Weatherimage"]
["currentobservation","WindChill"]
["currentobservation","Windd"]
["currentobservation","Winds"]
["currentobservation","elev"]
["currentobservation","id"]
["currentobservation","latitude"]
["currentobservation","longitude"]
["currentobservation","name"]
["currentobservation","state"]
["currentobservation","timezone"]
["data","hazard",[]]
["data","hazardUrl",[]]