Web Service: Saving JSON KEY as Embedded Data | XM Community
Skip to main content

Hello, in Survey Flow I have a web service where I am calling an API with a GET request. This works and returns JSON like this: Name:Color.
Susan:Purple
Bob:Green
Garett:Blue
etc. So the keys are dynamic.
Qualtrics will let you save values as embedded data if you know the keys.... but I don't. I need to save both keys and values as embedded data that I'm calling person like this:
person1name:Susan
person1color:Purple
person2name:Bob
person2color:Green
person3name:Garett
person3color:Blue
I can't figure this out? All the embedded data setting examples I find you have to know the key and you can only save the value.
Or if I could just save the whole response {"Susan":"Purple","Bob":"Green","Garett":"Blue"} and I can parse it in JS in the question I want to use it in.
Please help!
---
PS for anyone finding this in a search for trying to make basic authorization work in your web service, since I only found the answer in one buried post, it's, under custom headers, type Authorization on the left and then on the right it's Basic "base64encoded basic" So the right side will look like this Basic "asfaag22425"

It looks like your web service is returning a flat json object when it should have levels. It should return this:
{
Person: {
1: {
name: "Susan",
color: "Purple"
},
2: {
name: "Bob",
color: "Purple",
}
}
}
Then the embedded data fields returned to Qualtrics would be:
Person.1.name
Person.1.color
Person.2.name
Person.2.color


Hmm thank you for your reply! I agree and will ask them if they can change it. I really, really appreciate the example of what the embedded data will be when it's fixed.


Hello,
For future reference (or if anyone wants to clean up code), I can't get the web service owner to give me a leveled structure. What I have done as a workaround is put JavaScript directly on a question in the survey (right after I get the query data). I don't know if this is efficient, as I smashed it together from various website examples, but it works.

Qualtrics.SurveyEngine.addOnload(function()
{
   /*Place your JavaScript here to run when the page loads*/
    fetch("APIurl?query1=value"),
   {
   method: 'get',
   headers: new Headers({
       'Authorization': 'Basic xxxxx'
   })
   })
 // RETURN SERVER RESPONSE AS TEXT
 .then((result) => {
   console.log(result);
   if (result.status != 200) { throw new Error("Bad Server Response"); }
   return result.text();
 })
 // SAVE KEY VALUE PAIRS AS EMBEDDED DATA
 .then((response) => {
   const names_and_colors = JSON.parse(response);
   var i=1;
   for (const ekey, value] of Object.entries(names_and_colors)) {
   Qualtrics.SurveyEngine.setEmbeddedData("name"+i.toString(), key);
   Qualtrics.SurveyEngine.setEmbeddedData("color"+i.toString(), value);
++i;
}
});
});


Leave a Reply