Documentation Docs
Documentation Docs

Access Survey Results

This help topic describes how you can access, modify, and display individual question values and full survey results.

Access Individual Question Values

To access an individual question value, use the question's value property. You can assign a new value to it if you want to update the question value programmatically. Alternatively, you can call the Survey's getValue(questionName) and setValue(questionName, newValue) methods.

The following code configures an nps_score question and uses the API members described above to access the question's value:

import { Model } from "survey-core";

const surveyJson = {
  "elements": [{
    "type": "rating",
    "name": "nps_score",
    "title": "On a scale of zero to ten, how likely are you to recommend our product to a friend or colleague?",
    "rateMin": 0,
    "rateMax": 10,
  }]
};
const survey = new Model(surveyJson);
const npsQuestion = survey.getQuestionByName("nps_score");

console.log(npsQuestion.value);
// ===== or =====
// console.log(survey.getValue("nps_score"));

npsQuestion.value = 10;
// ===== or =====
// console.log(survey.setValue("nps_score", 10));

If you need to access an individual question value before or immediately after it is changed, handle the Survey's onValueChanging or onValueChanged events:

survey.onValueChanging.add((survey, { name, question, oldValue, value }) => {
  console.log(`The ${name} question value is about to change from ${oldValue} to ${value}.`);
  // You can redefine the `value` argument if you want to change the question value:
  // value = myNewValue;
});

survey.onValueChanged.add((survey, { name, question, value }) => {
  console.log(`The ${name} question value has changed to ${value}.`);
});

View Demo

Access Full Survey Results

To access full survey results, use the Survey's data property. It contains a JSON object with answered questions and has the following structure:

{
  "question1Name": "question1Value",
  "question2Name": "question2Value",
  // ...
  "questionNName": "questionNValue",
}

Depending on the question type, question values can also be nested objects or arrays that contain objects or primitive values:

{
  "question1Name": [ "value1", "value2", ... ],
  "question2Name": {
    "nestedQuestion1Name": "nestedQuestion1Value",
    "nestedQuestion2Name": "nestedQuestion2Value",
    // ...
  },
  // ...
}

The following code outputs the data property into the browser's console:

import { Model } from "survey-core";

const surveyJson = { ... };
const survey = new Model(surveyJson);

console.log(survey.data);

View Demo

If you need to get survey results as an array of JavaScript objects, call the Survey's getPlainData() method. The objects in the array have the following structure:

{
  name: string, // Question name
  title: string, // Question title
  value: any, // Question value
  displayValue: string, // Question value converted to a string
  isNode: Boolean, // `true` if the question contains more than one value
  // Details about the values. Present only if `isNode` is `true`
  data: Array<{ name, title, value, displayValue, isNode, data}>
}

The following code shows how to call the getPlainData() method and output the result into the browser's console:

console.log(survey.getPlainData());

Modify Survey Results

The previous section described the default format of survey results. If you want to use a different format, modify survey results in a custom function. For example, you can bring survey result objects to the following structure:

{
  name: string, // Question name
  title: string, // Question title
  value: any, // Question value
  displayValue: string // Question value converted to a string
}

The code below converts survey results to this structure when a user completes the survey:

survey.onComplete.add((survey) => {
  const resultData = [];
  for (const key in survey.data) {
    const question = survey.getQuestionByName(key);
    if (!!question) {
      const item = {
        name: key,
        value: question.value,
        title: question.displayValue,
        displayValue: question.displayValue
      };
      resultData.push(item);
    }
  }
  // ...
  // Send `resultData` to your web server
  // ...
});

See Also

Send feedback to the SurveyJS team

Need help? Visit our support page

Copyright © 2024 Devsoft Baltic OÜ. All rights reserved.

Your cookie settings

We use cookies on our site to make your browsing experience more convenient and personal. In some cases, they are essential to making the site work properly. By clicking "Accept All", you consent to the use of all cookies in accordance with our Terms of Use & Privacy Statement. However, you may visit "Cookie settings" to provide a controlled consent.

Your renewal subscription expires soon.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.

Your renewal subscription has expired.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.