gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:28:12 25 4
gpt4 key购买 nike

我正在寻找一种方法来找到从变量中获取给定值的完整键路径。我的输入来自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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com