【问题标题】:How to insert a key-value pair after a specified key in a JSON object using jq?如何使用 jq 在 JSON 对象中的指定键之后插入键值对?
【发布时间】:2018-03-31 15:50:32
【问题描述】:

我有一个这样的 JSON 文件

{
    "hierarchy": {
        "structure": {
            "card_11001": [
                "addCard_4111"
            ],
            "container_11006": [
                "mainContainer_11007",
                "subContainer_10016"
            ],
            "mainContainer_11007": [
                "paymentMethodList_10001"
            ],
            "orderWrap_10012": [
                "orderSummary_10005"
            ],
            "paymentMethodList_10001": [
                "card_11001",
                "placeOrder_10013"
            ],
            "root_10000": [
                "payNotice_11011",
                "payNotice_10020",
                "container_11006",
                "placeOrderResultAction_10004"
            ],
            "subContainer_10016": [
                "orderWrap_10012",
                "footer_10014"
            ]
        }
    }
}

我想插入

"offline_11018": [
    "instruction_908",
    "checkboxList_11019"
]

"mainContainer_11007""orderWrap_10012" 之间,所以我想要的结果应该是这样的:

{
    "hierarchy": {
        "structure": {
            "card_11001": [
                "addCard_4111"
            ],
            "container_11006": [
                "mainContainer_11007",
                "subContainer_10016"
            ],
            "mainContainer_11007": [
                "paymentMethodList_10001"
            ],
            "offline_11018": [
                "instruction_908",
                "checkboxList_11019"
            ],
            "orderWrap_10012": [
                "orderSummary_10005"
            ],
            "paymentMethodList_10001": [
                "card_11001",
                "placeOrder_10013"
            ],
            "root_10000": [
                "payNotice_11011",
                "payNotice_10020",
                "container_11006",
                "placeOrderResultAction_10004"
            ],
            "subContainer_10016": [
                "orderWrap_10012",
                "footer_10014"
            ]
        }
    }
}

我只知道我只能用

将它附加到文件末尾
jq --raw-output '.hierarchy.structure + {"offline_11018": ["instruction_908","checkboxList_11019"]}'

但这不是我想要的,我想将它插入到另外两个键之间。如何使用 jq 命令做到这一点?

【问题讨论】:

  • 对象中的键是无序的。只要将新键添加到正确的对象中,键存在于何处都没有关系。

标签: json insert edit key-value jq


【解决方案1】:

最简单的方法是使用to_entries.hierarchy.structure 转换为数组,执行插入,然后使用from_entries 重构对象,如下所示:

.hierarchy.structure |= (to_entries
  | ... as $ix
  | .[:$ix] + ($x | to_entries) + .[$ix:]
  | from_entries)

如果可能找不到定义插入点的项目,当然需要修改上面的草图。

indexof/1

这是一个有用的“def”,用于查找某些条件成立的最小索引:

# If the input is an array, emit the least index, $i, 
# for which `.[$i]|f` is truthy, otherwise emit null.
# Works similarly if the input is a JSON object.
def indexof(f):
  label $out
  | foreach .[] as $x (null; .+1;
      if ($x|f) then (.-1, break $out) else empty end) // null;

使用indexof的解决方案

将以上部分放在一起:

{"offline_11018": [ "instruction_908", "checkboxList_11019" ]} as $x
| .hierarchy.structure |= (to_entries
    | (1 + indexof( .value | index("mainContainer_11007") )) as $ix
    | .[:$ix] + ($x | to_entries) + .[$ix:]
    | from_entries)

【讨论】:

  • 我尝试了您的解决方案,但出现错误,请查看jqplay.org/s/YWyksyMM7V
  • 您粘贴在大纲中,而不是完整的解决方案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-13
  • 1970-01-01
  • 2023-03-12
  • 2017-04-12
相关资源
最近更新 更多