我有一个包含以下数据的json文件:
{
"Index" : {
"example_user" : {
"important_key" : "leave_me_alone","blah" : {
"more_stuff" : {
"ignore_this" : "and_this_too"
}
}
},"another_user" : {
"blah" : {
"more_stuff" : {
"ignore_this" : "and_this_too"
}
}
},"extra_person" : {
"blah" : {
"more_stuff" : {
"ignore_this" : "and_this_too"
}
}
}
}
}
如您所见,important_key
和another_user
中缺少extra_person
。
我的目标是有条件地在缺少的地方添加"imporant_key" : ""
,但 不 替换任何现有的 important_key
值。我使用的程序是https://stedolan.github.io/jq/中的JQ
(1.6)。
在jqplay.org
上多次尝试失败之后,并在互联网上进行了大量研究(重新)如何完成工作,我只能设法实现以下目标:
过滤器:.Index[]+={"important_key":"data"}
结果
{
"Index": {
"example_user": {
"important_key": "data","blah": {
"more_stuff": {
"ignore_this": "and_this_too"
}
}
},"another_user": {
"blah": {
"more_stuff": {
"ignore_this": "and_this_too"
}
},"important_key": "data"
},"extra_person": {
"blah": {
"more_stuff": {
"ignore_this": "and_this_too"
}
},"important_key": "data"
}
}
}
我知道+=
(过度)会写入任何现有键值(请注意example_user
)。我一直在尝试使用多个piped
过滤器/运算符,包括|=
和{
}
,但最后,上述结果是我能得到的最接近的结果。 / p>
(不幸的是,我似乎找不到所有东西都变得“疯狂”的jqplay代码片段)