Skip to main content
ahmedk927042
August 26, 2021
Question

Saving Multiple Page Survey Answers as a JSON Object

  • August 26, 2021
  • 9 replies
  • 0 views

Hi,

I am trying to save the user answers of multiple page survey interface in one local variable as a json object to save it to the database. I have rendered these multilevel questions from part of the json object like that:

{
"sections": [{
"sectionname": "1. Legal Information, Disclaimers and Acknowledgments",
"sectionlevel": 1,
"sectionparent": "root",
"questions": [{}]

},
{
"sectionname": "1.1 Legal information",
"sectionlevel": 2,
"sectionparent": "1. Legal Information, Disclaimers and Acknowledgments",
"sectioninstructions": "Test Instructions",
"questions": [{
"questiontitle": "Legal Terms of Use",
"questionnbr": "1.1.1",
"questioninstructions": "Test Instructions",
"questiontext": "Feugiat sed lectus vestibulum mattis ullamcorper. Tempor orci dapibus ultrices in. Pretium fusce id velit ut. Et magnis dis parturient montes nascetur. Lacus sed viverra tellus in. Consectetur purus ut faucibus pulvinar elementum integer enim. Lectus quam id leo in vitae turpis massa sed elementum. Sed nisi lacus sed viverra tellus in hac habitasse. In tellus integer feugiat scelerisque varius morbi enim nunc faucibus.",
"responsetype": "radiobutton",
"responseoptions": [{
"responsevalue": "1",
"responsetext": "Yes. I agree to the terms of use.",
"responselabel": ""
},
{
"responsevalue": "0",
"responsetext": "No. I do not agree.",
"responselabel": ""
}
],
"ismandatory": "yes",
"visibleif": "",
"answertext": ""
},
{
"questiontitle": "Privacy, Disclosure and Use of Data",
"questionnbr": "1.1.2",
"questioninstructions": "Test Instructions",
"responsetype": "radiobutton",
"responseoptions": [{
"responsevalue": "1",
"responsetext": "Yes. I agree to the terms and conditions.",
"responselabel": ""
},
{
"responsevalue": "0",
"responsetext": "No. I do not agree to the terms and conditions.",
"responselabel": ""
}
],
"ismandatory": "yes",
"visibleif": "",
"answertext": ""
}
]
},
{
"sectionname": "1.2 Disclaimers and Acknowledgements",
"sectionlevel": 2,
"sectionparent": "1. Legal Information, Disclaimers and Acknowledgments",
"sectioninstructions": "Test Instructions",
"questions": [{
"questiontitle": "Disclaimer",
"questionnbr": "1.2.1",
"questioninstructions": "Test Instructions",
"questiontext": "Feugiat sed lectus vestibulum mattis ullamcorper. Tempor orci dapibus ultrices in. Pretium fusce id velit ut. Et magnis dis parturient montes nascetur. Lacus sed viverra tellus in. Consectetur purus ut faucibus pulvinar elementum integer enim. Lectus quam id leo in vitae turpis massa sed elementum. Sed nisi lacus sed viverra tellus in hac habitasse. In tellus integer feugiat scelerisque varius morbi enim nunc faucibus.",
"responsetype": "radiobutton",
"responseoptions": [{
"responsevalue": "1",
"responsetext": "Yes. I agree.",
"responselabel": ""
},
{
"responsevalue": "0",
"responsetext": "No. I do not agree.",
"responselabel": ""
}
],
"ismandatory": "yes",
"visibleif": "",
"answertext": ""
},
{
"questiontitle": "Acknowledgement",
"questionnbr": "1.2.2",
"questioninstructions": "Test Instructions",
"responsetype": "radiobutton",
"responseoptions": [{
"responsevalue": "1",
"responsetext": "Yes. I acknowledge.",
"responselabel": ""
},
{
"responsevalue": "0",
"responsetext": "No. I do not accept the terms.",
"responselabel": ""
}
],
"ismandatory": "yes",
"visibleif": "",
"answertext": ""
}
]
}
]
}

i did nested for loop to show the interface like the picture below, I have created a code to render these questions successfully for each page and i am saving the questions for each page in a local variable and i am mapping the user answer for each field to answertext (in the json object), so it is mapping the value correctly but the challenge here when go to other page, the local variable which is holding the current questions with the answers will be updated with the new questions for the new page and i will not have the old answers any more. so i thought to save the all questions in a local variable, then map the answers to each question directly but i could not because it is hierarchy structure. so could you please share any thoughts please? 

This is part of code that i am applying to render the questions for each section using nested for loop, so here i am just testing on textfield object:

a!forEach(
items: local!questions,
expression: {
/*the start of the questions types*/
if(
fv!item.responsetype = "text",
a!textField(
value: fv!item.answertext,
saveInto: {
fv!item.answertext,
}
),
)})

I would appreciate any thoughts for that because this is an urgent task. thanks in advance!

    9 replies

    danny.verb
    August 27, 2021

    When you say 'multiple pages' are you referring to a wizard on the same Interface or multiple user inputs tasks? If everything is on the same interface, then you can abstract your dictionary a little more by having a map
    a!map(

         pageOne: local!jsonOne,

         pageTwo: local!jsonTwo

    )

    ahmedk927042
    August 27, 2021

    Hi Danny,

    Thank you for your response! The survey has a hierarchy structure like in the Json above like (children and parents relationship). but any way i already built it by nested loop on the json. but the challenge here how i am going to map all of the answers for all of the subsections of the survey in one place. till now i can map each page answers with a local variable but when i move to another subsection, the local variable will have a new value of the new page questions. I am trying to collect all of the answers in one local variable to save it in a database table as a json object. any thoughts to achieve that?

    danny.verb
    August 27, 2021

    You can use the sql type TEXT as your column and store the JSON in there. I know you say you're looping through the JSON. I'm not sure what you mean but the simplest way to handle JSON in Appian is to use a map/dictionary for everything and then to use the a!toJSON() and a!fromJSON() functions

    mikes0011
    Brainy
    August 27, 2021

    Tangentially, this is a **lot** of code to paste into a post in plaintext.  You should probably use a Code Box, which makes things like this 100x readable through length limiting, monospaced font, and preservation of indentation/etc.

    The Expression Guru
    ahmedk927042
    August 27, 2021

    Hi Mike! Thank you for your response! I am adding here the same code i added above to save the value for each response.

    a!forEach(
    items: local!questions,
    expression: {
    /*the start of the questions types*/
    if(
    fv!item.responsetype = "text",
    a!textField(
    value: fv!item.answertext,
    saveInto: {
    fv!item.answertext,
    }
    ),
    )})

    mikes0011
    Brainy
    August 27, 2021

    To clarify, if you copy/paste indented code straight from Appian into a code box, indentation will be preserved as well.

    The Expression Guru