希望大家都好。
我正在努力使用 JQ 尝试使用特定键来选择输入 json 中的字符串值数组。 "key":["string1",..., "stringn"] 可以嵌入 json 中任意深度的“某处”。我只知道数组的键值。
假设我有一些 json,其中包括键字典 (dict) 和 json 源 (source)。在此示例中,我想选择“key4”和“key11”数组,并将它们放入带有相应键的结果 json 中。
我的示例输入是:
{
"dict": ["key4", "key11"],
"source":{
"key0": {
"key1": "valueA",
"key2": 123456,
"key3": [{
"key4": ["anotherValue4341", "anotherValue4342"],
"key5": [{
"someKey351": "someValue351"
}, {
"someKey352": "someValue352"
}],
"key6": 999
},
{
"key7": "anotherValue342",
"key8": "anotherValue352",
"key9": 666
}
],
"key10": {
"key11": ["lastvalue111", "lastvalue112", "lastvalue113"]
}
}
}}
我对此示例的预期输出是:
{
"key4": ["anotherValue4341", "anotherValue4342"],
"key11": ["lastvalue111", "lastvalue112", "lastvalue113"]
}
我正在使用 JQ 来提取请求的输出。
现在我尝试重用以前的查询来选择键/值,如下所示:
jq '.dict as $dict | .source | reduce paths as $p (.;getpath($p) as $v| if $v|type == "string" and $dict[$v] then setpath($p; $dict[$v]) else . end)'
但它似乎在处理某些值时遇到了困难:jq: error (at :26): Cannot index array with string "valueA"
我还尝试选择包含 dict 中的键的匹配对象:
jq '.dict as $dict | .source | recurse(.[]?) | objects | select(in($dict))'
但这会导致错误“无法检查数组是否有对象键”
我希望我能够清楚地解释我的需求/问题。
任何提示表示赞赏。
请您参考如下方法:
为了简单起见,我们首先假设核心任务是获取与 一个特定的键。为了说明和清晰起见,让我们相应地定义一个函数:
# Emit a (possibly empty) stream of key-value objects
# corresponding to the $key specified as a string
def getKeyValue($key):
.. | objects | select(has($key)) | {($key): .[$key]};
问题的解决方案现在很简单:
[.dict[] as $k | getKeyValue($k)] | add
变体
这个解决方案有几个潜在的问题:
输入中可能根本没有出现一个或多个感兴趣的键;
一个或多个感兴趣的键可能在输入中出现多次;
如果输入很大,那么更有效的解决方案可能会更好。
前两个问题很容易解决,但细节取决于具体的要求。 同样可以通过修改 getKeyValue 的 def 来轻松解决效率问题,以便 命名参数是一个字符串数组。

