Retrieve Response API + Code task to parse the JSON issue
Hi! Has anyone been able to use the Retrieve Survey Response Web Service task together with the Code task? I'm trying to get the JSON from a response, then convert it into readable text to send via an Email task, but the result keeps being empty.
The API is working:
The JS code is very simple:
function codeTask() { // Retrieve the JSON content let jsonResponse = "~{ch://OCAC_VAR4RGrFLH6Ekqf/$.result}";
// Parse the JSON content let parsedResponse = JSON.parse(jsonResponse);
// Convert the parsed JSON into readable text (this can be customized based on the structure of your JSON) let readableText = JSON.stringify(parsedResponse, null, 2); // Indented with 2 spaces for readability
// Return the readable text return { result: readableText }; }
The Email task is supposed to show the result, but turns out empty:
What am I doing wrong?
Page 1 / 1
@JohannesCE
Try using below code:
function codeTask() { // Retrieve the JSON content let jsonResponse = `~{ch://OCAC_VAR4RGrFLH6Ekqf/$.result}`;
// Parse the JSON content let parsedResponse = JSON.parse(jsonResponse);
// Convert the parsed JSON into readable text (this can be customized based on the structure of your JSON) let readableText = JSON.stringify(parsedResponse, null, 2); // Indented with 2 spaces for readability
return { result: readableText }; }
The inverted commas were the error fyi.
Hope it helps!
Thank you @Deepak, that works!
However, as soon as I try to add my styling, the result is empty.
Basically I just want the code to transform the JSON into readable and styled HTML that I can pipe into an email task.
Here as I test I simply try to take various parts of the JSON and display them in HTML
function codeTask() { let displayedFields = `~(ch://OCAC_VAR4RGrFLH6Ekqf/$.result.displayedFields)`; let labels = `~(ch://OCAC_VAR4RGrFLH6Ekqf/$.result.labels)`; let values = `~{ch://OCAC_VAR4RGrFLH6Ekqf/$.result.values}`;
After many attempts I found out I need to use arrays. Like this:
function codeTask() { // Retrieve the labels and values from the JSON content let labels = JSON.parse(`~{ch://***/$.result.labels}`); let values = JSON.parse(`~{ch://***/$.result.values}`);
// Initialize an empty array to store the formatted content let combinedText = [];
// Iterate through each label and its corresponding value for (let key in labels) { let contentArray = [ `<strong>`, labels[key], `</strong>: `, values[key] || 'N/A', `<br>` ]; combinedText.push(contentArray.join('')); }
// Wrap the combinedText in a div with the desired styling let styledTextArray = [ `<div style="background-color: #F0F0F0; padding: 10px; font-family: Arial, sans-serif; color: #192743;">`, ...combinedText, `</div>` ];
return { result: styledTextArray.join('') }; }
@JohannesCE
Glad, you were able to figure this out.
@Deepak I thought I was out of the woods, but no
So the code in the previous post works, but only for small surveys.
Apparently when the survey is large the API call in the Web Service works, but only retuns a “$.result” field with a long JSON.
The code task then can't seem to handle the amount of text or to parse the JSON.
So now my issue is: how to retreive and parse a response for a big survey with many open questions? With the following code I tried to get only the first 10 questions, but it doesn't work. I'm not sure if there is an error in the code or if whenever the JSON is too large the whole workflow just fails...
function codeTask() { // Retrieve the entire JSON content let jsonResponse = JSON.parse(`~{ch://OCAC_3dRmetCRWce8y9f/$.result}`);
// Extract labels and values let labels = jsonResponse.labels; let values = jsonResponse.values;
// Initialize an empty array to store the formatted content let combinedText = i];
// Get the first 10 keys from the labels object let first10Keys = Object.keys(labels).slice(0, 10);
// Iterate through each of the first 10 labels and their corresponding values for (let key of first10Keys) { let contentArray = e `<strong>`, labels key], `</strong>: `, values key] || 'N/A', `<br>` ]; combinedText.push(contentArray.join('')); }
// Wrap the combinedText in a div with the desired styling let styledTextArray = e `<div style="background-color: #F0F0F0; padding: 10px; font-family: Arial, sans-serif; color: #192743;">`, ...combinedText, `</div>` ];