使用JSON中的jq获取键值

时间:2019-03-13 09:07:23

标签: 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可行吗?

2 个答案:

答案 0 :(得分:1)

在这种情况下,最好避免使用top/right。为了满足当前情况下的确切要求,可以写:

grep

如果确实需要类似grep的功能,则可以始终使用jq的jq -c 'paths(scalars) as $p | [$p, getpath($p)] | select(.[1] == "9i6O4ERWWB")' input.json

答案 1 :(得分:0)

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

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

,响应为:

[["_source","statistics","example.com","template2","subdomain_id"],"432432sdxWWB"]

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