json - 使用 jq 获取选定对象的父键
问题描述
我在这里问了一个关于从 JSON 结构中获取键和值的问题:Using jq to get key and value from JSON
这是 JSON:
{
"63": {
"state": {
"on": false,
"alert": "select",
"mode": "automation",
"reachable": true
},
"swupdate": {
"state": "notupdatable",
"lastinstall": "2019-09-15T11:19:15"
},
"type": "plug",
"name": "Tree",
"modelid": "XXX",
"manufacturername": "XXX",
"productname": "plug",
"capabilities": {
"certified": false,
"control": {},
"streaming": {
"renderer": false,
"proxy": false
}
},
"config": {
"archetype": "plug",
"function": "functional",
"direction": "omnidirectional"
},
"uniqueid": "00:0d:6f:ff:fe:da:c9:dc-01",
"swversion": "2.0.022"
}
}
我只想根据 .name 的内容发出对象的父键:其中 .name == "Tree",返回 "63"。
我可以发出整个对象:
jq -r '.[] | select(.name == "Tree")'
或键和名称列表:
jq -r 'map_values(.name)'
解决方案
您可以使用键/值格式格式化 JSONto_entries()
并获取与某个值对应的键
jq --raw-output 'to_entries[] | select(.value.name == "Tree").key'
推荐阅读
- alloy - 如何将此信号添加到合金中的关系中?
- css - 使用 Java 以编程方式在 Vaadin 14 中通过 CSS 指定背景图像
- c++ - 从类对象调用函数时出现致命错误
- javascript - FB Instant Game 资产加载;停留在 0%
- python - 用 df2 中一列中的值减去 df1 中所有列的值
- c# - C# 中不区分大小写的文本搜索:突出显示匹配短语时如何保持原始大小写?
- java - System.out.format("height is %3.1f percent greater than length\n", percent); 是什么意思?用Java做?
- javafx - TableView updateItem 上的 setRowFactory - 样式 -fx-font-weight:粗体不起作用
- javascript - 如何使用硬件键盘键触发 html 按钮及其 onclick 功能?
- javascript - 谷歌优化防闪烁片段超时