[英]How to remove an empty object from a json file
I have a json file that looks like this:我有一个看起来像这样的 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": {}
}
]
]
]
I'd like to remove the empty arrays like timetracking and components.我想删除空的 arrays ,例如时间跟踪和组件。
I was reading a lot on line and tried different things, but I only manage to remove null or empty values, but the whole array or object.我在网上阅读了很多内容并尝试了不同的东西,但我只设法删除 null 或空值,但整个数组或 object。
This is what I tried:这是我尝试过的:
cat $FilePathOrigin | jq-win64.exe -sc 'fromstream(tostream | select(length == 1 or .[1] != null))' > $FilePathDestiny
As observed in the comments, it's not entirely clear what you want, but if you want to remove the keys that have values equal to [] or {}, then you could use walk
like so:正如评论中所观察到的,您想要什么并不完全清楚,但是如果您想删除值等于 [] 或 {} 的键,那么您可以像这样使用walk
:
walk(if type == "object"
then with_entries(if .value == {} or .value == [] then empty else . end)
else . end)
This can also be written more compactly as:这也可以更简洁地写成:
walk(if type == "object" then with_entries(select(.value | (. != {} and . != []))) else . end)
If you only want to remove keys from specific objects, then the simplest would probably be to use with_entries
as above.如果您只想从特定对象中删除键,那么最简单的方法可能是使用上面的with_entries
。
length
关于length
的警告length
is not only defined on arrays and objects! length
不仅在 arrays 和对象上定义!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.