Question T10247
Visible to All Users

Change or Add Json field on output json

created 2 years ago

Hi,

i'm very interested to use radio group : beautiful, compact and easy to design (no need heavy Trigger) and i have lot question like this in my survey…

But i need a specific key name in json output for each choice.

Clipboard-File-1.png

Actually the outpout json is :

{ "question1": "item1" }

I hope :

{ "item1": "1" } but this `{ "question1": "item1", "item1": "1" } would be ok too.

so i think a custom JS function is the solution wich test the question type (radiogroup), but OnComplete is too late i think.

What would the best strategy ?

Answers approved by surveyjs Support

created 2 years ago

Hello,
You can do it onComplete. How do you want to define the item key? Do you want to have a new property or it is an index of the selected choice?

Thank you,
Andrew
SurveyJS Team

    Show previous comments (3)

      Hello Remy,
      OK, let's say that you have a function that do it, in this case you can modify JSON you send to your server onComplete event.
      Here is the code and the working example:

      JavaScript
      survey.onComplete.add(function (sender) { const data = sender.data; const selectedItems = {}; for(var key in data) { const q = sender.getQuestionByValueName(key); if(q.getType() === "radiogroup" && !!q.selectedItem) { const index = q.choices.indexOf(q.selectedItem); selectedItems[q.selectedItem.value] = index; } } for(var key in selectedItems) { data[key] = selectedItems[key]; } document.querySelector('#surveyResult').textContent = "Result JSON:\n" + JSON.stringify(data, null, 3); });

      Thank you,
      Andrew
      SurveyJS Team

        Many thanks Andrew

        What beautiful code !

        I derived it to use it also with checkbox with "maxSelectedChoices": 1, and it run right.

        Just 2 questions :

        • to get the checkbox item value name : i use this expression => q.selectedItems[0].propertyHash.value
          is there something more pretty ?
        • to treat multiple choices in the checkbox, i must make a loop on the result array ?
        JavaScript
        Survey.StylesManager.applyTheme("defaultV2"); var json = { "elements": [ { "type": "checkbox", "name": "checkbox_question", "choices": [ "item1", "item2", "item3" ], "colCount": 3, "maxSelectedChoices": 1 }, { "type": "radiogroup", "name": "radiogroup_question", "choices": [ { "value": "field1", "text": "tata" }, { "value": "field2", "text": "toto" }, { "value": "field3", "text": "titi" } ], "colCount": 3 } ] }; window.survey = new Survey.Model(json); survey.onComplete.add(function (sender) { const data = sender.data; const selectedItems = {}; for(var key in data) { const q = sender.getQuestionByValueName(key); if (q.getType() === "radiogroup" && !! q.selectedItem) { const index = "1"; selectedItems[q.selectedItem.value] = index; } else if (q.getType() === "checkbox" && !! q.selectedItems) { console.log(q.selectedItems[0].propertyHash.value); const index = "1"; selectedItems[q.selectedItems[0].propertyHash.value] = index; } } for(var key in selectedItems) { data[key] = selectedItems[key]; } document.querySelector('#surveyResult').textContent = "Result JSON:\n" + JSON.stringify(data, null, 3); }); ReactDOM.render (<SurveyReact.Survey model={survey}/>, document.getElementById("surveyElement"));

          Hello,
          You can use the following code: q.selectedItems[0].value.
          Here is the updated example.

          JavaScript
          function setSelectedItem(selectedItems, q, item) { const index = q.choices.indexOf(item); selectedItems[item.value] = index; } survey.onComplete.add(function (sender) { const data = sender.data; const selectedItems = {}; for (var key in data) { const q = sender.getQuestionByValueName(key); if (q.getType() === 'radiogroup' && !!q.selectedItem) { setSelectedItem(selectedItems, q, q.selectedItem); } else { if (q.getType() === 'checkbox' && Array.isArray(q.selectedItems)) { for (var i = 0; i < q.selectedItems.length; i++) { setSelectedItem(selectedItems, q, q.selectedItems[i]); } } } } for (var key in selectedItems) { data[key] = selectedItems[key]; } document.querySelector('#surveyResult').textContent = 'Result JSON:\n' + JSON.stringify(data, null, 3); });

          Thank you,
          Andrew
          SurveyJS Team

          Other Answers

          created 2 years ago

          Thanks a lot Andrew for your help, it's precious !

          Rémy

            Comments (1)

              You are welcome!

              Andrew
              SurveyJS Team
              If you like our products, please take a moment to share your experience with our company by contributing a short review for SurveyJS page at g2.com or at Trustpilot.com.
              As we’re building our business, every review helps build credibility for future customers and your feedback would be greatly appreciated.