0

I have a successful payload from my API call. Successful Return Payload:
{status:“success”, state:”QLD”, Code:”123”, offerName:”EOFY Deal”, offerDiscount:”30%”, offerStartDate:”2020-06-01” , offerEndDate:”2020-06-30”}

What's the best way to use this Json file as my dynamic content using AMPscript in emails. The offer only pertains to recipients who live in the state, and are linked to this code. Recipients are stored in their own DE.

I thought of moving the JSON file into a DE_Offer and link it to the Recipient table. Refresh the DE_offer on a daily basis, by calling the API but looks like that doesn't work.

What's the best way to call the offer data in Real Time and use it at the time of send for eligible recipients?

HttpsGet?

0

So I would not recommend JSON parsing via AMPScript. I would utilize Serve-Side JavaScript (SSJS) or Guide Template Language (GTL) to do this. I almost exclusively recommend SSJS as I feel it gives you more options and capabilities, but it also has a bit heavier overhead to learn and use.

Here is an example in SSJS:

<script runat=server>
Platform.Load("Core","1");

var url = 'http://www.example.com'
var resp = HTTP.Get(url);
var status = resp.status;
var state = resp.state;
//etc...
</script>

You can then either use the SSJS to AMPScript var functions or use the SSJS var outputs <ctrl:var name=status /> to push your vars into the email.

The other option is GTL:

%%[ SET @Json = '{status:"success", state:"QLD", Code:"123", offerName:"EOFY Deal", offerDiscount:"30%", offerStartDate:"2020-06-01" , offerEndDate:"2020-06-30"}' ]%%

{{.datasource JSONVar type=variable maxRows = 20}}
     {{.data}}
    { "target" : "@Json" }
     {{/data}}
     Status: {{status}}
     State: {{state}}
     Code: {{Code}}
     OfferName: {{offerName}}
{{/datasource}}

Which would then output the vars from the JSON.

AMPScript does not natively have any way to parse through JSON, you would be forced to manually cut into it via RegEx and/or Substring, IndexOf, etc. Which can be dicey and usually requires a lot of upfront effort and caveats and business rules to ensure it functions as expected.

| improve this answer | |
  • Thank you. This is great. One last question when I use GTL, I still need to separately make the API call and manually add the payload in the Set @Json variable for it to work? – Kim Nov 20 at 23:08
  • @Kim correct. You would make the call via AMPScript and use that var instead of the one I showed manually filled in – Gortonington Nov 20 at 23:15
  • Thank you much appreciated. – Kim Nov 21 at 22:39
  • @Kim - if this answered your question please mark it as accepted (checkmark next to the answer) so others can more easily find the solution if they face the same issue. – Gortonington Nov 23 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.