3

I have a form and want to add the submission values to a data extension. This is what the payload looks like:

{
    "emailType": "Business",
     "title": "Founder",
     "firstName": "Walter",
    
}

I have tried the following code but my ParseJSON function is returning undefined. Is there something I'm missing here?

<script runat="server" language="javascript">
  
Platform.Load("Core","1.1.1");
var jsonpost = Platform.Request.GetPostData()
var jsonparse = Platform.Function.ParseJSON(jsonpost);

var emailtype = jsonparse.emailtype

Write("jsonpost: "+jsonpost);
Write("<br>jsonparse: " +jsonparse);
Write("<br>emailtype: " +emailtype);

This is what I receive:

jsonpost: "{\r\n    \"emailType\": \"Business\",\r\n     \"title\": \"Founder\",\r\n     \"firstName\": \"Walter\",\r\n    \r\n}"

jsonparse: undefined
emailtype: undefined
2
  • If you hard-code the JSONpost data in your page, does it parse correctly? Looks like a source form-data encoding issue to me. May 27, 2021 at 14:52
  • 2 things, First - for your var emailtype = jsonparse.emailtype it should be var emailtype = jsonparse.emailType (SSJS is case sensitive). SECOND - when writing your jsonparse, you should wrap it in a Stringify() Write("<br>jsonparse: " + Stringify(jsonparse)); Doing those changes, produced the right output for me. May 27, 2021 at 16:12

1 Answer 1

5

So I did a test on a landing page in SFMC using your code and noticed 2 things:

  1. For your var emailtype = jsonparse.emailtype it should be var emailtype = jsonparse.emailType (SSJS is case sensitive).
  2. When writing your jsonparse, you should wrap it in a Stringify() Write("<br>jsonparse: " + Stringify(jsonparse));

Doing those changes produced the right output for me. I tried it both via a POST to the page from POSTman and by hard coding your example request json from above in the page.

FROM POSTman:

jsonpost: {
"emailType": "Business",
"title": "Founder",
"firstName": "Walter",

}<br>jsonparse: {"emailType":"Business","title":"Founder","firstName":"Walter"}<br>emailtype: Business

From hardcoded in page:

jsonpost: { "emailType": "Business", "title": "Founder", "firstName": "Walter", }
jsonparse: {"emailType":"Business","title":"Founder","firstName":"Walter"}
emailtype: Business 

The JSON you provided is not proper syntax either with that added comma at the end of 'firstName', but it seems that the ParseJSON is smart enough to account for that and properly adjust.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .