[英]How to remove an empty object from a json file
我有一個看起來像這樣的 json 文件:
[
[
[
{
"expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
"id": "10000",
"self": "https://test.atlassian.net/rest/api/latest/issue/10000",
"key": "AGILE-1",
"components": [],
"timetracking": {}
}
]
]
]
我想刪除空的 arrays ,例如時間跟蹤和組件。
我在網上閱讀了很多內容並嘗試了不同的東西,但我只設法刪除 null 或空值,但整個數組或 object。
這是我嘗試過的:
cat $FilePathOrigin | jq-win64.exe -sc 'fromstream(tostream | select(length == 1 or .[1] != null))' > $FilePathDestiny
正如評論中所觀察到的,您想要什么並不完全清楚,但是如果您想刪除值等於 [] 或 {} 的鍵,那么您可以像這樣使用walk
:
walk(if type == "object"
then with_entries(if .value == {} or .value == [] then empty else . end)
else . end)
這也可以更簡潔地寫成:
walk(if type == "object" then with_entries(select(.value | (. != {} and . != []))) else . end)
如果您只想從特定對象中刪除鍵,那么最簡單的方法可能是使用上面的with_entries
。
length
的警告 length
不僅在 arrays 和對象上定義!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.