JSON string saved as embedded data | XM Community
Solved

JSON string saved as embedded data

  • 6 June 2020
  • 4 replies
  • 165 views

Hello,
I'm having a strange issue parsing JSON in JavaScript within a Qualtrics question. The following code works fine:
var obj = '{"name":"John","age":30,"city":"New York"}';
document.getElementById("json").innerHTML = JSON.parse(obj).age;
That is, it shows "30" for the question, corresponding to the age in the JSON.
But weirdly, this doesn't work:
var obj = '${e://Field/message}';
document.getElementById("json").innerHTML = JSON.parse(obj).age;
Here, the embedded data stored in the "message" variable is the exact same string stored in "obj" in the first code block. JSON.parse, however, fails to run. I'm a bit puzzled given that the strings seem to be literally identical; however, I figure that the embedded data must be being parsed in some strange way. I thought somebody here might know what's going on.
Many thanks.

icon

Best answer by rondev 6 June 2020, 15:54

View original

4 replies

Userlevel 7
Badge +22

adambear91 , I would request you to check if the message embedded data contains exact same data, because I duplicated your case, and it was working as expected.

Thanks rondev , you are right that there was an important detail I left out that turns out to make a difference. If I just copy and paste the JSON string and save as embedded data in Survey Flow, it works. I assume this is what you tried.
However, I was saving the embedded data from a web service. Specifically, the string is in a Qualtrics message body, and I was using Qualtrics' API to download that message body. I'm positive that the message is typographically identical to what I was pasting in here, and I even tried removing spaces in JavaScript to make sure that extra spaces weren't somehow being saved when converting from the message to embedded data. So there must be something strange about the way that the web service data is saved that's invisible, since it comes out identically in the HTML.

Userlevel 7
Badge +22

Can you try doing console.log of the message embedded data and check what is displayed?

Thanks again rondev. Weirdly, I just tried everything again, and it now works! Not sure what was going on, but I appreciate your help.

Leave a Reply