【问题标题】:Add new array element to an existing JSON array using jq for sendgrid API使用 jq for sendgrid API 将新数组元素添加到现有 JSON 数组
【发布时间】:2017-11-01 11:32:57
【问题描述】:

这个问题与previous 相关。

就我而言,我想简单地添加单个元素。我有以下 input.json。

{
  "content": [
  ],
  "from": {
    "email": "someemail@gmail.com",
    "name": "Some Name"
  },
  "reply_to": {
    "email": "someemail@gmail.com",
    "name": "Some Name"
    },
  "personalizations": [{
    "to": [{
        "email": "someemail@gmail.com"
      },
      {
        "email": "someotheremail@gmail.com"
      }]
    }]
}

我想附加主题和模板 ID,以便 output.json 如下所示。

{
  "content": [
  ],
  "from": {
    "email": "someemail@gmail.com",
    "name": "Some Name"
  },
  "reply_to": {
    "email": "someemail@gmail.com",
    "name": "Some Name"
    },
  "personalizations": [{
    "to": [{
        "email": "someemail@gmail.com"
      },
      {
        "email": "someotheremail@gmail.com"
      }],
    "subject": "Some subject"
    }],
    "template_id": "someID"
}

我将如何在 bash 中使用 jq(包括 input.json 和 output.json 的语法)来做到这一点?

【问题讨论】:

    标签: json bash sendgrid jq


    【解决方案1】:

    简单的赋值:

    jq '.template_id="someID" | .personalizations[0].subject="Some subject"' input.json
    

    输出:

    {
      "content": [],
      "from": {
        "email": "someemail@gmail.com",
        "name": "Some Name"
      },
      "reply_to": {
        "email": "someemail@gmail.com",
        "name": "Some Name"
      },
      "personalizations": [
        {
          "to": [
            {
              "email": "someemail@gmail.com"
            },
            {
              "email": "someotheremail@gmail.com"
            }
          ],
          "subject": "Some subject"
        }
      ],
      "template_id": "someID"
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-03
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      相关资源
      最近更新 更多