0

Here's the nested json I'm working with

{
   "Instructions":{
      "Instruction":[
         {
            "Type 1":"abc",
            "Type 2":"def",
            "Registrations":{
               "Registration":[
                  "xyz",
                  "abc"
               ]
            },
            "Type 3":"abc",
            "Type 4":"def"
         },
         {
            "Type 1":"abc",
            "Type 2":"xyz",
            "Registrations":{
               "Registration":[
                  "xyz",
                  "abc "
               ]
            },
            "Type 3":"abc",
            "Type 4":"def"
         },
         {
            "Type 1":"abc",
            "Type 2":" ",
            "Registrations":{
               "Registration":[
                  
               ]
            },
            "Type 3":"abc",
            "Type 4":"def"
         }
      ]
   }
}

and using nested for loop to retrieve it in the email and code is working fine for the non null values but it's completely skipping through null value if registration is null. For example I want to display Type 1, Type 2 and Type 3 even if registration is null. Here's the code

<table border = "0" cellpadding="0" cellspacing="0" role="0" width="100%">
<tr>
    <td>
        <table role="" style="width:100%">
            <tr>
                <th align="left" style="font-weight:bold; border-bottom: 1px #cbd1d6;">Account Ending</th>
                <th align="center" style="font-weight:bold; border-bottom: 1px #cbd1d6;">Distribution Type</th>
                <th align="center" style="font-weight:bold; border-bottom: 1px #cbd1d6;">Registrations</th>
            </tr>
            <script runat="server" language="javascript">
                Platform.Load("Core", "1");

                var JsonStr = Attribute.GetValue("Data");
                //Json Prep
                var json;
                try {
                    Json = Platform.Functions.ParseJSON(JsonStr);
                    if (debug) {
                    }
                }
                catch (e) {
                    Platform.Functions.RaiseError("unable to parse Data JSON", true)
                }
                var Instructionso = Json.Instructions;
                var InstructionsArr = [];
                if (Instructionso && Object.prototype.toString.call(Instructionso) === '[object Array]') {
                    InstructionsArr = Instructionso;
                }
                elseif(Instructionso) {
                    InstructionsArr.push(Instructionso);
                }
                // InstructionsArr Loop
                for (var i = 0; i < InstructionsArr.length; i++) {

                    var Instructiono = InstructionsArr[i].Instruction;
                    var InstructionArr = [];
                    if (Instructiono && Object.prototype.toString.call(Instructiono) === '[object Array]') {
                        InstructionArr = Instructiono;
                    }
                    elseif(Instructiono) {
                        InstructionArr.push(Instructiono);
                    }
            </script>
            // InstructionArr Loop
            <script runat="server" language="javascript">
for (var k = 0; k < InstructionArr.length; k++) {
    var Type 1 = InstructionArr[k].Type 1;
    var Type 2 = InstructionArr[k].Type 2;
    var Type 3 = InstructionArr[k].Type 3;
    var Type 4 = InstructionArr[k].Type 4;
    Platform.Variable.SetValue("@Type 1", Type 1);
    Platform.Variable.SetValue("@Type 2", Type 2);
    Platform.Variable.SetValue("@Type 3", Type 3);
    Platform.Variable.SetValue("@Type 4", Type 4);
    var Registrationso = InstructionArr[k].Registrationso;
    var RegistrationsArr = [];
    if (Registrationso && Object.prototype.toString.call(Registrationso) === '[object Array]') {
        RegistrationsArr = Registrationso;
    }
    elseif(Registrationso) {
        RegistrationsArr.push(Registrationso);

    }
//RegistrationsArr loop

            </script>
            <script runat="server" language="javascript">
                        for (var m = 0; m < RegistrationsArr.length; m++) {
                            var Registrationo = RegistrationsArr[m].Registration;
                            var RegistrationArr = [];
                            if (Registrationo && Object.prototype.toString.call(Registrationo) === '[object Array]') {
                                RegistrationArr = Registrationo;
                            }
                            elseif(Registrationo) {
                                RegistrationArr.push(Registrationo);

                            }
                            for (var n = 0; m < RegistrationArr.length n++) {
                                var Registration = RegistrationArr[m];
                                Platform.Variable.SetValue("@Registration", Registration);
            </script>
            <tr>
                <td align="left" style=" font-weight:normal; border-bottom: 1px #98A4AE;">
                    %%[IF EMPTY(@Account_num) THEN]%%
                    %%[ELSE]%%
                    %%=v(@Account_num)=%%
                    %%[ENDIF]%%
                </td>
                <td align="left" style=" font-weight:normal; border-bottom: 1px #98A4AE;">
                    %%[IF EMPTY(@Type 1) THEN]%%
                    %%[ELSE]%%
                    %%=v(@Type 1)=%%
                    %%[ENDIF]%% <br>
                    %%[IF EMPTY(@Type 2) THEN]%%
                    %%[ELSE]%%
                    %%=v(@Type 2)=%%
                    %%[ENDIF]%%
                </td>
                <td align="left" style=" font-weight:normal; border-bottom: 1px #98A4AE;">
                    %%[IF EMPTY(@Type 3) THEN]%%
                    %%[ELSE]%%
                    %%=v(@Type 3)=%%
                    %%[ENDIF]%%
                    %%[IF EMPTY(@Registration) THEN]%%
                    %%[ELSE]%%
                    %%=v(@Registration)=%%
                    %%[ENDIF]%%
                    %%[IF EMPTY(@Type 4) THEN]%%
                    %%[ELSE]%%
                    %%=v(@Type 4)=%%
                    %%[ENDIF]%%

                </td>
                <script runat="server" language="javascript"></script>
                }
                }
                }
                }
                </script>

            </tr>
        </table>
    </td>
</tr>

I have to display the values in the table format. If I change the location of the Registration loop, Type 1 variable is getting displayed but then it's totally messing up the expected output. Now I'm getting everything correctly except when Registrations is empty it's skipping the whole row even though when Instruction has value like Type 1 should be displayed but not getting it.Please help me with this. Any help would be greatly appreciated. Thank you.

6
  • 1
    Please don't paste screenshots of your code. Paste actual code instead Commented Mar 22, 2023 at 6:06
  • Also please paste the code that outputs the @Registration and @Type1 variables to the email body - the problem might actually be there Commented Mar 22, 2023 at 10:19
  • @Lukas Lunow I have edited my question. I really need help on this, I have been stuck on this from weeks. Any help would be greatly appreciated. Thank you
    – zahaan
    Commented Mar 22, 2023 at 20:50
  • @Rafal Wolsztyniak I have added the code as you mentioned. I would really appreciate your help. Thank you.
    – zahaan
    Commented Mar 22, 2023 at 20:51
  • Still - sharing the actual JSON instead of a screenshot will be preferred Commented Mar 22, 2023 at 20:55

0

You must log in to answer this question.

Browse other questions tagged .