bash - 使用JSON中的jq获取键值

标签 bash elasticsearch jq

我正在寻找一种方法来找到从变量中获取给定值的完整键路径。我的输入来自elasticsearch查询结果。

例如,我想要键值的完整路径: 9i6O4ERWWB
它们的键值始终是唯一的,唯一的变化是example.com和template1键(我无法预测名称是什么)。

一旦知道了关键路径:
_source.example.com.template1 我想增加“计数器”字段并更新elasticsearch文档。

我的输入JSON:

{
    "_index": "domains",
    "_type": "doc",
    "_id": "c66443eb1e6a0850b03a91fdb967f4d1",
    "_score": 2.4877305,
    "_source": {
        "user_id": "c66443eb1e6a0850b03a91fdb967f4d1",
        "statistics": {
            "test_count": 0,
            "datasize": 0,
            "example.com": {
                "template1": {
                    "image_id": "iPpDWbaO3YTIEb0pBkW3.png",
                    "link_id": "4ybOOUJpaBpDaLxPkz1j.html",
                    "counter": 0,
                    "subdomain_id": "9i6O4ERWWB"
                },
                "template2": {
                    "image_id": "iPpDWasdas322sdaW3.png",
                    "link_id": "4ybOOd3425sdfsz1j.html",
                    "counter": 1,
                    "subdomain_id": "432432sdxWWB"
                }
            },
            "example1.com": {
                "template1": {
                    "image_id": "iPpDWdasdasdasdas3.png",
                    "link_id": "4ybOOUadsasdadsasd1j.html",
                    "subdomain_id": "9i6O4ERWWB"
                }
            }
        }
    }
}

我尝试过的是:
<myfile jq -c 'paths | select(.[-1])
<myfile jq -c 'paths | select(.[-1] == "subdomain_id")'

但这会打印所有键值:
["_index"]
["_type"]
["_id"]
["_score"]
["_source"]
["_source","user_id"]
["_source","statistics"]
["_source","statistics","test_count"]
["_source","statistics","datasize"]
["_source","statistics","example.com"]
["_source","statistics","example.com","template1"]
["_source","statistics","example.com","template1","image_id"]
["_source","statistics","example.com","template1","link_id"]
["_source","statistics","example.com","template1","subdomain_id"]
["_source","statistics","template2"]
["_source","statistics","template2","image_id"]
["_source","statistics","template2","link_id"]
["_source","statistics","template2","subdomain_id"]
["_source","statistics","example1.com"]
["_source","statistics","example1.com","template1"]
["_source","statistics","example1.com","template1","image_id"]
["_source","statistics","example1.com","template1","link_id"]
["_source","statistics","example1.com","template1","subdomain_id"]

我想写的伪代码:
seeked_key_value="432432sdxWWB"
jq -n --arg seeked_key_value "$seeked_key_value" \
'paths | select(.[-1].$seeked_key_value'

预期结果:["_source","statistics","example.com","template1","subdomain_id":"432432sdxWWB"]
这对bash中的jq可行吗?

最佳答案

您可以使用以下方法“提取”路径:

jq -c 'paths(scalars) as $p | [$p, getpath($p)]' file.json | grep 432432sdxWWB

响应是:
[["_source","statistics","example.com","template2","subdomain_id"],"432432sdxWWB"]

可能您可以改进jq查询以仅获取单个值,但希望它可以帮助您确定最终版本:)

关于bash - 使用JSON中的jq获取键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55137976/

相关文章:

bash - Jenkins : Environment variables not available in CMake

elasticsearch - 弹性查询以搜索术语并在日期范围内

elasticsearch - 嵌套在elasticsearch中的嵌套聚合中

database - Elasticsearch 通过 "Partial Term"而不是 "Entire Term"进行聚合

json - 设置多个值

bash - 在 bash 中的 while 循环中添加超时命令时出错

linux - 如何在 bash shell 中编写精美优雅的 linux 命令

json - jq 加入公共(public)键

json - 如何在 Powershell 中使用 jq 写入 JSON 文件而不更改二进制文件?

bash - 如何将多个文件通过管道传输到 ffmpeg?