如何从JSON获取父节点?

问题描述 投票:0回答:1

[稍微更新一下我的问题]

我在 test.json 文件中有以下 JSON。我正在尝试找出如何获取所有根名称,这些根名称的值本质上是完全动态的。

{
       "result_set": {
        "/api/device/4756": {
            "name": "Arbitrator",
            "ip": "127.0.0.1",
            "hostname": "acme.arbitrate.com"
        },
        "/api/device/4757": {
            "name": "Auditor",
            "ip": "127.0.0.2",
            "hostname": "acme.auditor.com"
        },
        "/api/device/4758": {
            "name": "Augumentor",
            "ip": "127.0.0.3",
            "hostname": "acme.augumentor.com"
        }
    }
}

到目前为止,我已经尝试过,但它没有返回我正在寻找的内容......

cat test.json | jq -r '["names"], (.result_set | [])'

我正在寻找如下所示的结果...

/api/device/4756, Arbitrator, 127.0.0.1, acme.arbitrate.com
/api/device/4757, Auditor, 127.0.0.2, acme.auditor.com
/api/device/4758, Augumentor, 127.0.0.3, acme.augumentor.com
jq
1个回答
0
投票

是否可以进一步增强此功能以提供以下输出?

/api/device/4756, Arbitrator, 127.0.0.1, acme.arbitrate.com 
/api/device/4757, Auditor, 127.0.0.2, acme.auditor.com
/api/device/4758, Augumentor, 127.0.0.3, acme.augumentor.com

使用

to_entries
来代替,它将对象分解为键值对列表:

.result_set | to_entries[] | [.key, .value.name, .value.ip, .value.hostname]
| join(", ") # or @csv
© www.soinside.com 2019 - 2024. All rights reserved.