cancel
Showing results for 
Search instead for 
Did you mean: 

Looping through a json array to show data as Custom message in CAI chatbot

0 Kudos

I have been trying to loop through a json array and show data in the form of list using custom message but I have been getting an error Message added is not a valid JSON.


{
  "type": "list",
  "content": {
    "elements": [
     {{#eachJoin api_service_response.default.body.d.results}}
      {
        "title": "{{Incident_No}}",
        "imageUrl": "",
        "description": "{{Incident_Title}}",
        "buttons": [
          {
            "title": "Details",
            "value": "{{Incident_No}}",
            "type": "postback"
          }
        ]
      }{{/eachJoin}}
    ]
  }
}
{
        "level": "warning",
        "code": "W_EXECUTE_ACTION",
        "data": {
          "type": "message",
          "value": {
            "invalid_json": "{\"type\":\"script\",\"content\":\"{\n  \"type\": \"list\",\n  \"content\": {\n    \"elements\": [\n     {{#eachJoin api_service_response.default.body.d.results}}\n      {\n        \"title\": \"{{Incident_No}}\",\n        \"imageUrl\": \"\",\n        \"description\": \"{{Incident_Title}}\",\n        \"buttons\": [\n          {\n            \"title\": \"Details\",\n            \"value\": \"{{Incident_No}}\"\n          }\n        ]\n      }{{/eachJoin}}\n    ]\n  }\n}\",\"messageType\":\"MessageList\",\"isContentStateEdited\":true,\"delay\":null}",
            "scripting_enabled": false
          },
          "message": "Message added is not a valid JSON"
        },
        "timestamp": "2021-03-22T05:49:36.100Z"
      }
mateuszadamus
Active Contributor
0 Kudos

Hello

How do you have your return variable defined in the body of the return JSON? Is it really default (no name given)?

Kind regards,
Mateusz
0 Kudos
"api_service_response": {
        "default": {
                "body": { . . . },
                "status_code": 200,
                "headers": [ . . . ]
        }
}

I have returned it like this and the data coming in json format is:

https://pq6q6nqoztycwwojnovationdemo-apis.cfapps.eu10.hana.ondemand.com/odata/SunPh_Incidents.xsodat...

mateuszadamus
Active Contributor
0 Kudos

Do you check the STATUS_CODE of the request? Are you sure it's 200 and the JSON is returned successfully?


Kind regards,
Mateusz

Accepted Solutions (1)

Accepted Solutions (1)

Dan_Wroblewski
Developer Advocate
Developer Advocate

Are you sure you are referring to the results of your API correctly? I took your exact code, changing only to use Northwind service, and it worked perfectly. How are you defining the API call in the chatbot? What does the call return?

0 Kudos

Hi Daniel,

Can you please share the code for north wind that you configured in the chatbot?

I think I am missing some syntax or maybe typing the wrong path. Once I will get your code, it will be clear to me where I was going wrong.

Requesting your help in this, please.

Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Kudos

I believe there is an issue with the actual data. If I use only the incident number, it works, but if I use the incident title, it does not. I assume this is because the incident title has some texts like this:

"During Filling of product \"Duoz Respules\" Bottelpack Machine 460 M. (Make Rommelag) moves in Unsterile mode."

and this is messing up the JSON. Depending on what you want to happen, you will need to encode the data or parse it.

0 Kudos

Hi Daniel,

Could you please share your exact code of the custom message like I did?

I am not able to show incident number like you did, that's why I am asking.

Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Kudos

I literally cut and pasted your code, but then simply did not use the title field:

{
  "type": "list",
  "content": {
    "elements": [
     {{#eachJoin api_service_response.default.body.d.results}}
      {
        "title": "{{Incident_No}}",
        "imageUrl": "",
        "description": "{{Incident_No}}",
        "buttons": [
          {
            "title": "Details",
            "value": "{{Incident_No}}",
            "type": "postback"
          }
        ]
      }{{/eachJoin}}
    ]
  }
}

Can you show how you defined the "External API" in your action, including the response tab?

Dan_Wroblewski
Developer Advocate
Developer Advocate

You can escape the title and it will work, though not 100% sure it will be what you want:

        "description": "{{escape Incident_Title}}",
Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Kudos

It looks OK -- did you try my code (without Incident_Title or with escape before)?

0 Kudos

Thank you so much Daniel ,

"description": "{{escape Incident_Title}}"

It has solved the problem.

Answers (0)