Question T8984
Visible to All Users

Display custom json result

created 3 years ago

I have create a survey like this:

JSON
{ "pages": [ { "name": "page1", "elements": [ { "type": "radiogroup", "name": "question1", "title": "Do you have a learning disability?", "category": "Disabilities", "choices": [ { "value": "yes", "text": "Yes" }, { "value": "no", "text": "No" } ] } ] }, { "name": "page2", "elements": [ { "type": "radiogroup", "name": "question2", "title": "Do you need financial aid?", "category": "Financials", "choices": [ { "value": "yes", "text": "Yes" }, { "value": "no", "text": "No" } ] } ] }, { "name": "page3", "elements": [ { "type": "radiogroup", "name": "question3", "title": "Do you require a ride from the airport?", "category": "Rides", "choices": [ { "value": "yes", "text": "Yes" }, { "value": "no", "text": "No" } ] } ] } ] }

and get a result like this:

JSON
result: {"question1":{"value":"yes","category":"Disabilities"},"question2":{"value":"yes","category":"Financials"},"question3":{"value":"yes","category":"Rides"}}

and now I want to show this result in survey so I do this

Code
survey.data = result; survey.mode = 'display';

But all questions are empty!!

what should I do? can you give me an example?

thanks

Answers approved by surveyjs Support

created 3 years ago

Hello Amir,
You have to convert to survey JSON data format.

JavaScript
const result = {     question1: { value'yes'category'Disabilities' },     question2: { value'yes'category'Financials' },     question3: { value'yes'category'Rides' }, }; const surveyResult = {}; for(var key in result) {     surveyResult[key] = result[key].value; } survey.data = surveyResult; survey.mode = 'display';

Thank you,
Andrew
SurveyJS Team

    Show previous comments (1)

      Hello,
      What do you mean?
      Survey model expect their data in the following format: { questionName: questionValue, … }. You can't provide it with another format. Your category parameter can be added later onComplete event.

      Thank you,
      Andrew
      SurveyJS Team

        I need to keep other information such as category in addition to the value when reviewing questions in display mode .

          Your result variable is not going to change and since it is a display mode, your survey data will not change as well.
          I do not see any problem with keeping my code. IMHO, you are trying to solve the issue that doesn't exist.

          Thank you,
          Andrew
          SurveyJS Team