如果有 none,请不要使用 JQ 注入属性
Don't inject attribute with JQ if there was none
cat "$FileName" | jq --sort-keys 'map({"author_id": .author.id,"author": .author.name, "badge": .author.badges[0].title, "message", "timestamp", "time_in_seconds", "time_text"})' > "$TargetName"
如果源 JSON 中没有 "time_in_seconds"
, 将生成带有 "time_in_seconds": null
的输出。如何消除这个:
- 对于这个属性?
- 所有属性?
要删除一个字段,如果它是 null
,请使用 .time_in_seconds |= select(.)
。
要删除所有 null
的字段,请使用 .[] |= select(.)
。
最后在 map
中添加这个,如下所示:
jq 'map({…} | .[] |= select(.))'
注意:这也将删除值为 false
的字段。如果要限制为 null
,请使 select(.)
更明确并将其更改为 select(. != null)
.
要删除输入中的所有 null
值(或任意条件),您可以使用 recurse/0
(..
) 和 del/1
.
del(.. | select(. == null))
cat "$FileName" | jq --sort-keys 'map({"author_id": .author.id,"author": .author.name, "badge": .author.badges[0].title, "message", "timestamp", "time_in_seconds", "time_text"})' > "$TargetName"
如果源 JSON 中没有 "time_in_seconds"
, 将生成带有 "time_in_seconds": null
的输出。如何消除这个:
- 对于这个属性?
- 所有属性?
要删除一个字段,如果它是 null
,请使用 .time_in_seconds |= select(.)
。
要删除所有 null
的字段,请使用 .[] |= select(.)
。
最后在 map
中添加这个,如下所示:
jq 'map({…} | .[] |= select(.))'
注意:这也将删除值为 false
的字段。如果要限制为 null
,请使 select(.)
更明确并将其更改为 select(. != null)
.
要删除输入中的所有 null
值(或任意条件),您可以使用 recurse/0
(..
) 和 del/1
.
del(.. | select(. == null))