我的 json 檔案包含整數陣列、字串和物件。有沒有辦法壓縮只包含整數或字串的陣列的輸出?
- 要么不顯示這些陣列的元素,要么
- 顯示元素型別和計數
這就是它現在的樣子:
$ jq "." foo.json
{
"version": [
"2.53.0",
"2.53.0",
"2.53.0",
"2.53.0",
"2.53.0",
"2.53.3",
"2.53.3",
"2.53.0",
"2.53.0",
"2.53.3",
"2.53.0",
"2.53.0",
"2.53.3",
"2.53.0",
"2.53.0",
"2.53.0",
"2.53.0",
"2.53.0",
"2.53.3",
"2.53.0"
],
"walltime_seconds": [
0.165,
0.199,
0.415,
0.193,
12.114,
0.227,
12.341,
12.145,
0.135,
0.326,
0.293,
0.19,
0.271,
0.103,
0.196,
0.18,
0.177,
0.166,
0.506,
0.568
]
}
這就是我想要的:
{
"version": "[..]",
"walltime_seconds": "[..]",
}
或這個
{
"version": "Array(str, 20)",
"walltime_seconds": "Array(float, 20)",
}
當然,壓縮應該發生在 json 樹中的任何地方,并且應該只針對 int、str、float 而不是物件進行。
uj5u.com熱心網友回復:
這將做到:
def compress:
if type == "array"
then (if all(.[]; type == "string") then "[string]"
elif all(.[]; type == "number") then "[number]"
elif all(.[]; type == "boolean") then "[boolean]"
else .
end)
else .
end;
walk(compress)
使用您的示例輸入,結果將是:
{
"version": "[string]",
"walltime_seconds": "[number]"
}
要包含 的長度和句柄陣列null:
def compress:
def check($t):
if all(.[]; type == $t) then "[\($t)[\(length)]]" else empty end;
if type == "array"
then check("string") // check("number") // check("boolean") // check("null") // .
else .
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/462240.html
