1

This question is related to a previous one.

In my case, I would like to simply add individual elements. I have the following input.json.

{
  "content": [
  ],
  "from": {
    "email": "[email protected]",
    "name": "Some Name"
  },
  "reply_to": {
    "email": "[email protected]",
    "name": "Some Name"
    },
  "personalizations": [{
    "to": [{
        "email": "[email protected]"
      },
      {
        "email": "[email protected]"
      }]
    }]
}

I would like to append the subject and template_id so that the output.json looks like below.

{
  "content": [
  ],
  "from": {
    "email": "[email protected]",
    "name": "Some Name"
  },
  "reply_to": {
    "email": "[email protected]",
    "name": "Some Name"
    },
  "personalizations": [{
    "to": [{
        "email": "[email protected]"
      },
      {
        "email": "[email protected]"
      }],
    "subject": "Some subject"
    }],
    "template_id": "someID"
}

How would I do that with jq (including the syntax for input.json and output.json) in bash?

1 Answer 1

1

With simple assignment:

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

The output:

{
  "content": [],
  "from": {
    "email": "[email protected]",
    "name": "Some Name"
  },
  "reply_to": {
    "email": "[email protected]",
    "name": "Some Name"
  },
  "personalizations": [
    {
      "to": [
        {
          "email": "[email protected]"
        },
        {
          "email": "[email protected]"
        }
      ],
      "subject": "Some subject"
    }
  ],
  "template_id": "someID"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.