I need to merge JSON objects generated with AMPScript inside SSJS and write it to an AMPScript variable.

AMPScript + JavaScript code:

      VAR @JSON1,@JSON2
      SET @JSON1 = "{"car":{"brand":"Ford","Model":"Fusion"}"
      SET @JSON2 = "{"car":{"brand":"Chevrolet","Model":"Camaro"}"

  <script runat="server">
      Platform.Load("Core", "1");
      var jsonString1 = Variable.GetValue("@JSON1"); /* but I don't think that I am been able to get the object */
      var obj1 = JSON.parse(jsonString1);
      var obj1View = Variable.SetValue("@jsonString1")
      var jsonString2 = Variable.GetValue("@JSON2");
      var obj2 = JSON.parse(jsonString2);
      var jsonObj = Object.assign(obj1,obj2);
      var mergeJsonObjt = Variable.SetValue("@jsonObj");

Email (HTML/AMPscript):

<!DOCTYPE html>
  <meta charset="UTF-8">
  • There are a few things broken there in the code. Before the specifics, what are you going to do with the JSON object once you're back to AMPScript? I'm assuming you're looking for an array or something at the end containing two objects - Fusion and Camaro? – Macca May 31 '17 at 8:02
  • After I get this merge object: {"car":{"brand":"Ford","Model":"Fusion"},"car":{"brand":"Chevrolet","Model":"Camaro"}}, I will do a for loop of this JSON and feed my email with the output of it. – Felipe Jordani May 31 '17 at 8:14
  • I'm not sure that answered either of my questions. I'll post a reply to what I think you're trying to do. – Macca May 31 '17 at 8:24
  • @AndrewMcLeod I'm trying to get two object using ampscript, send them to javascript, merge them, and do a for loop in the objects to send the value of his keys to my Email temapate. – Felipe Jordani May 31 '17 at 8:28
  • What would this object look like? What does "Merge" mean in this context? Looping's something you'd more likely do with an array. Are you planning on using Guide Template Language to consume this object - AMPScript can't deal with JSON? If not GTL, why are you bothering going back to AMPScript, rather than just use SSJS all the way? – Macca May 31 '17 at 8:36

It's not entirely clear what type of object you want back in AMPScript or what you'll do with it, but this should populate @Array with a JSON string representing an array containing your two car objects.

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

  var arr = [];

  var jsonString1 = Variable.GetValue("@JSON1");

  var jsonString2 = Variable.GetValue("@JSON2");

| improve this answer | |
  • Thanks, I used your code plus a function to merge the object and is working as I needed >> var jsonString1 = Variable.GetValue("@JSON1"); var obj1 = Platform.Function.ParseJSON(jsonString1); var jsonString2 = Variable.GetValue("@JSON2"); var obj2 = Platform.Function.ParseJSON(jsonString2); function extend(obj, src) {for (var key in src) {if (src.hasOwnProperty(key)) obj[key] = src[key];} return obj;} var jsonObj = extend(obj1, obj2); – Felipe Jordani May 31 '17 at 9:43
  • Awesome! Good to hear you're on the move again. – Macca May 31 '17 at 9:46

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.