0

Tryin to use jq to make a slight transformation in a JSON document I am working with.

I am using this as sample input JSON:

{"root" : {"to" : {"email" : "[email protected]"}, "dynamic_template_data" : {"FIRSTNAME" : "BLANCA", "last_4" : "9999", "OFFERCODE" : "88888888888888888"}}}
{"root" : {"to" : {"email" : "[email protected]"}, "dynamic_template_data" : {"FIRSTNAME" : "Michael", "last_4" : "8888", "OFFERCODE" : "9999999999999999999999"}}}

At the moment, I am using this jq filter to try to transform the JSON (also specifying slurp option):

jq --slurp '{"template_id":"d-34d9948687504d1f91fa9ddb738e94c0","asm":{"group_id":14817},"categories":["PROD","ICAP"],"personalizations": [.[].root],"from" : {"email" : "[email protected]", "name" : "Bank of the World"}, "reply_to" : {"email" : "[email protected]", "name" : "Bank of the World"}}'

...which returns close to what I need below with the "to" element to be an array containing the email address:

{
  "template_id": "d-34d9948687504d1f91fa9ddb738e94c0",
  "asm": {
    "group_id": 14817
  },
  "categories": [
    "PROD",
    "ICAP"
  ],
  "personalizations": [
    {
      "to": {
        "email": "[email protected]"
      },
      "dynamic_template_data": {
        "FIRSTNAME": "BLANCA",
        "last_4": "9999",
        "OFFERCODE": "88888888888888888"
      }
    },
    {
      "to": {
        "email": "[email protected]"
      },
      "dynamic_template_data": {
        "FIRSTNAME": "Michael",
        "last_4": "8888",
        "OFFERCODE": "9999999999999999999999"
      }
    }
  ],
  "from": {
    "email": "[email protected]",
    "name": "Bank of the World"
  },
  "reply_to": {
    "email": "[email protected]",
    "name": "Bank of the World"
  }
}

...but the email address is not returning in an array as I need.

Desired output:

{
"template_id": "d-34d9948687504d1f91fa9ddb738e94c0",
"asm": {"group_id": 14817},
"categories": [
    "PROD",
    "ICAP"
],
"personalizations": [
    {
        "to": [
            {"email": "[email protected]"}
        ],
        "dynamic_template_data": {
            "FIRSTNAME": "BLANCA",
            "last_4": "9999",
            "OFFERCODE": "88888888888888888"
        }
    },
    {
        "to": [
            {"email": "[email protected]"}
        ],
        "dynamic_template_data": {
            "FIRSTNAME": "Michael",
            "last_4": "8888",
            "OFFERCODE": "9999999999999999999999"
        }
    }
],
"from": {
    "email": "[email protected]",
    "name": "Bank of the World"
},
"reply_to": {
    "email": "[email protected]",
    "name": "Bank of the World"
}
}

1

1 Answer 1

1

Replace .to with [.to]

jq --slurp '{"personalizations": [.[].root | .to = [.to]]}'
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.