json - 使用 JQ 提取数据
问题描述
我正在尝试使用 jq 从包含特定值的 JSON 字段中提取数据。
这是我正在使用的 JSON:
{
"thing1": {
"a": false,
"b": true,
"c": false,
"d": false
},
"thing2": {
"a": true,
"b": true,
"c": true,
"d": true
},
"thing3": {
"a": true,
"b": false,
"c": false,
"d": false
}
}
我想保留包含"true"
值的对象,并删除值不是的键true
。
我对 JQ 没有太多经验,也没有如何正确构造 json 数据。如果这意味着重构主要的 JSON 数据,我也愿意这样做。
这是我想要达到的结果:
{
"thing1": {
"b": true
},
"thing2": {
"a": true,
"b": true,
"c": true,
"d": true
},
"thing3": {
"a": true
}
}
解决方案
# First remove the subkeys that are not truthy:
map_values(with_entries(select(.value)))
# ... then remove the empty dictionaries:
| with_entries(select(.value|length > 0))
加入调味料调味。
推荐阅读
- python - Python:返回每月第三个星期四的下一个出现日期
- python-3.x - 使用 GridSearchCV 制作自定义记分器
- android - 是否可以在不依赖 kotlin-stdlib 的情况下在 JVM 或 Android 上使用 kotlin?
- reactjs - handleSubmit 函数中的 this.setState 未重置表单输入
- wpf - HOWTO:当鼠标位于 WPF 边框的边和角之一时触发事件
- android - Android Studio 说我不在 gradle build 上,但我在
- scala - 我怎样才能让这个理解返回?
- javascript - 值不能从 ReadableNativeMap 转换为 double
- php - 在标签和 str_replace() 之间查找文本
- css - CSS - Visual Composer 覆盖样式未应用我在 Chrome 控制台中看到的内容