1

Essentially, I am wanting to pass the below request body and in cloud page, add to a data extension but unsure how I might be able to achieve this.

{
"id": "id1",
"formid" : "formid",
"data":[ {"column_name":"Full Name","string_value":"John Doe"},
                     {"column_name":"User Phone", "string_value":"12345678"},
                     {"column_name":"User Email", "string_value":"abc@123.com"}],
}

I've tested using the below and response was 200OK, just not sure what the next steps might be and seeking guidance if anyone has done something similar.

<script runat="server" language="javascript">
Platform.Load("Core","1");
var jsonpost = Platform.Request.GetPostData()
var json = Platform.Function.ParseJSON(jsonpost);
Write("<br>message: " +json.msg);  
Write("<br>jsonpost: "+jsonpost);
Write("<br>json: "+json);
</script>

Updated
After running the request, the response body is as below:

<br>message: undefined
<br>jsonpost: {
"id": "id1",
"formid" : "formid",
"data":[ {"column_name":"Full Name","string_value":"John Doe"},
                     {"column_name":"User Phone", "string_value":"12345678"},
                     {"column_name":"User Email", "string_value":"abc@123.com"}],
<br>json:
7
  • pls add information about your DE: fields, primary key(s). this will be important to debug insertion into the data extension. In general you can work with SSJS's data extension functions like InsertData and UpsertData. See developer.salesforce.com/docs/… and developer.salesforce.com/docs/… Sep 10 '20 at 7:03
  • Hi @Jonas, DE is just firstname, lastname, email, email being primary key. How would i use the upsert data?
    – s3002068
    Sep 10 '20 at 11:55
  • 1
    A couple questions: 1. Is this being POSTed in (validating if GetPostData() is correct)? Is it being POSTed as a string (validating ParseJSON)? the parsed JSON object - does it have the label/attribute of msg that contains the displayed JSON in your post? Lastly, I would also recommend writing the jsonpost and json vars as well to see where it falls over. Sep 10 '20 at 15:08
  • Hi @Gortonington, yes it is POST, response status is 200 OK. I added the write for jsonpost and json vars and updated my question to show the response body. Looks like the jsonpost is just pullingrequest body as is. Not sure what I should be doing here.
    – s3002068
    Sep 10 '20 at 23:26
  • 1
    Looks like json is your actual object. So can use that to iterate through. E. G. var data = json.data would pull your array out. Might need to parsejson on that as well before can use. Sep 10 '20 at 23:37
1

here is some example code to play around with on a Cloudpage. I have taken your input from GetPostData and put it in as a string for ease of testing.

Mind you that this example only works if you the json contains ONE dataset in the payload, because of the structure of the json.

it gets more complicated with several records at once, as you will have to iterate through it. Not sure if that is a requirement, you'd have to specify.

also be advised your input data is not formatted like your data extension from the comment. As per comment you want firstname and lastname separate, but your json delivers them together. This has some potential to give you a headache (double names...).

So I would suggest setting up your DE to match the JSON precisely (>> one field "full name" instead of two separate ones). I am assuming this in my last line of code where I commented out an example Upsert into a DE with fields: email (PK), fullName, Phone.

<script runat="server" language="javascript">
Platform.Load("Core","1");
var jsonpost = '{"id": "id1","formid" : "formid","data":[ {"column_name":"Full Name","string_value":"John Doe"},{"column_name":"User Phone", "string_value":"12345678"},{"column_name":"User Email", "string_value":"abc@123.com"}],}'
var json = Platform.Function.ParseJSON(jsonpost);

Write("<br>jsonpost: "+jsonpost);
Write("<br>parsed and stringified json: "+Stringify(json));
  
var lastname = json.data[0].string_value
var firstname = json.data[1].string_value
var email = json.data[2].string_value
Write("<br>fullName: "+ json.data[0].string_value)
Write("<br>phone: "+ json.data[1].string_value)
Write("<br>email: "+ json.data[2].string_value)
  
//var yourDE = "THIS IS A DATA EXTENSION NAME";
//var rows = Platform.Function.UpsertData(yourDE,["email"],[email],["fullName","phone"],[fullName,phone]);

</script>

the result is displayed as:

jsonpost: {"id": "id1","formid" : "formid","data":[ {"column_name":"Full Name","string_value":"John Doe"},{"column_name":"User Phone", "string_value":"12345678"},{"column_name":"User Email", "string_value":"abc@123.com"}],}
parsed and stringified json: {"id":"id1","formid":"formid","data":[{"column_name":"Full Name","string_value":"John Doe"}, {"column_name":"User Phone","string_value":"12345678"}, {"column_name":"User Email","string_value":"abc@123.com"}]}
fullName: John Doe
phone 1: 12345678
email: abc@123.com 

hope this is a starting point.

1
  • This is great, thanks so much!
    – s3002068
    Sep 12 '20 at 2:39

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.