如何使用jq命令添加多个json对象?

时间:2019-09-02 22:44:20

标签: json jq

我要编辑此json文件:

{
    "shipping_orders": [{
    }],
    "timestamp": 100,
    "shoporders": [{
        "buyer_remark": null
    }],
    "payment_channel_info": {
        "shipping_method": 28,
        "grouping_info": {
            "groups": [{
                "display_info": {
                    "discount": 1
                }
            }]
        }
    }
}

我想将这些json对象添加到文件中

  1. "headers": {}
  2. "ext_ad_info_mappings": []
  3. "enabled": true

我想要的输出是这样的:

{
    "headers": {},
    "shipping_orders": [{
    }],
    "timestamp": 100,
    "shoporders": [{
        "buyer_remark": null,
        "ext_ad_info_mappings": []
    }],
    "payment_channel_info": {
        "shipping_method": 28,
        "grouping_info": {
            "groups": [{
                "display_info": {
                    "discount": 1,
                    "enabled": true
                }
            }]
        }
    }
}

我尝试了此命令,但结果仅显示true https://jqplay.org/s/0aVbFzMttK

jq '.+{"headers": {}} and .shoporders[] + {"ext_ad_info_mappings": []} and .payment_channel_info.grouping_info.groups[].display_info + {"enabled": true}'

我应该如何编辑json文件,使其输出像我上面想要的那样?

1 个答案:

答案 0 :(得分:0)

只需在树中要设置值的位置进行设置即可。

.headers = {}
  | .shoporders[].ext_ad_info_mappings = []
  | .payment_channel_info.grouping_info.groups[].display_info.enabled = true

https://jqplay.org/s/ugF5foO90N