Skip to Content
Technical Articles
Author's profile photo Daniel Wroblewski

In SAP Conversational AI, can scripting accomplish anything?

Scripting inside SAP Conversational AI chatbots is a key feature, enabling developers to parse API responses or convert bits of conversations to display the way they want.

The developers in the community ask how to do all kinds of crazy things within scripting. Well, not really crazy, but developers are used to doing whatever they want in standard coding languages, and admittedly, the scripting available for chatbots can be limiting.

But I like a challenge, and I like to believe that you can accomplish anything if you are a little creative. Here’s a recent example that was given to me.

The Challenge

The developer had an API that returned a JSON array of objects, as follows:

[
   {
      "CustomerID": "123123",
      "CustomerName": "Aerosmith"
   },
   {
      "CustomerID": "123123",
      "CustomerName": "Aerosmith"
   },
   {
      "CustomerID": "999999",
      "CustomerName": "AC/DC"
   },
]

But the developer wanted to take the JSON and get rid of duplicates. Duplicates were defined as multiple objects with the same CustomerID, whether or not the Customer Name is the same.

SAP Conversational AI scripting has a unique helper function, but this only works on a simple array of strings or numbers. At first I tried to store an array of unique customer IDs, which is easy to do, but this did not help me.

I was stumped

My Solution

To be honest I don’t remember how I came across this idea, but I took it in 2 steps.

First, I took the array (which I had stored in memory under myarray), and created from it an object with a series of attributes, each attribute being the ID of a different customer. I used the following code:

{
    {{#eachJoin memory.myarray}}
       "{{this.CustomerID}}" : {{this}}
    {{/eachJoin}}
}

The secret is that you cannot have an object with 2 attributes with the same name. So when there was a duplicate (or triplicate), the new attribute would simply overwrite the previous one with the same name. Now I had something like this:

[
  { 
    "123123" : {
         "CustomerID": "123123",
         "CustomerName": "Aerosmith6"
          },
   "999999" : {
         "CustomerID": "999999",
         "CustomerName": "AC/DC"
         }
  }
]

The second and last step was simply to reformat the object in its original array form.

[
   {{#eachJoin memory.myarray2}}
      {{this}}
   {{/eachJoin}}
]

Let me know if you’ve had any challenges with scripting and how you solved them.

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo kratika varshney
      kratika varshney

      Hello Daniel Wroblewski,

      You blog is very nice. I had tried many scripting syntaxes. But I am facing some issues. i have following json in my memory and i want to do the sum of cost for every unique prod_id. can you please help. Thanks in Advance

      [
        {
          "Prod_Id": "002",
          "cost": " 4803600.000"
        },
        {
          "Prod_Id": "004",
          "cost": " 3706404.000"
        },
        {
          "Prod_Id": "004",
          "cost": " 2784950.000"
        },
        {
          "Prod_Id": "004",
          "cost": " 4756485.000"
        },
        {
          "Prod_Id": "001",
          "cost": " 3226647.000"
        },
        {
          "Prod_Id": "002",
          "cost": " 6429540.800"
        },
        {
          "Prod_Id": "002",
          "cost": " 5521200.000"
        },
        {
          "Prod_Id": "002",
          "cost": " 5113184.050"
        },
        {
          "Prod_Id": "002",
          "cost": " 5286549.000"
        },
        {
          "Prod_Id": "002",
          "cost": " 3871875.000"
        },
        {
          "Prod_Id": "004",
          "cost": " 3093649.000"
        },
        {
          "Prod_Id": "004",
          "cost": " 5249386.000"
        },
        {
          "Prod_Id": "004",
          "cost": " 3093649.000"
        },
        {
          "Prod_Id": "006",
          "cost": " 3108563.000"
        },
        {
          "Prod_Id": "014",
          "cost": " 1765800.000"
        },
        {
          "Prod_Id": "006",
          "cost": " 2596000.000"
        },
        {
          "Prod_Id": "005",
          "cost": " 2484733.330"
        },
        {
          "Prod_Id": "002",
          "cost": " 7757350.000"
        },
        {
          "Prod_Id": "006",
          "cost": " 2396000.000"
        },
        {
          "Prod_Id": "018",
          "cost": " 1143069.000"
        },
        {
          "Prod_Id": "018",
          "cost": " 2010400.000"
        },
        {
          "Prod_Id": "002",
          "cost": " 3871875.000"
        },
        {
          "Prod_Id": "005",
          "cost": " 1529123.250"
        },
        {
          "Prod_Id": "014",
          "cost": " 2500000.000"
        },
        {
          "Prod_Id": "014",
          "cost": " 2500000.000"
        },
        {
          "Prod_Id": "006",
          "cost": " 3256538.000"
        },
        {
          "Prod_Id": "006",
          "cost": " 2476000.000"
        },
        {
          "Prod_Id": "006",
          "cost": " 2439810.000"
        },
        {
          "Prod_Id": "018",
          "cost": " 1042857.000"
        },
        {
          "Prod_Id": "018",
          "cost": " 1605334.000"
        },
        {
          "Prod_Id": "006",
          "cost": " 3356894.000"
        },
        {
          "Prod_Id": "014",
          "cost": " 2689600.000"
        },
        {
          "Prod_Id": "006",
          "cost": " 2490840.000"
        },
        {
          "Prod_Id": "006",
          "cost": " 2657952.000"
        },
        {
          "Prod_Id": "006",
          "cost": " 2596000.000"
        },
        {
          "Prod_Id": "006",
          "cost": " 2476000.000"
        },
        {
          "Prod_Id": "004",
          "cost": " 3707112.000"
        }
      ]
      Author's profile photo Daniel Wroblewski
      Daniel Wroblewski
      Blog Post Author

      You mean you want in the end to have something like this:

      [{

      [{
          "Prod_Id": "001",
          "cost": "<total of all of the 006 entires>"
        },{
          "Prod_Id": "002",
          "cost": "<total of all of the 006 entires>"
        },{
          "Prod_Id": "003",
          "cost": "<total of all of the 006 entires>"
        },
      
      ....
      
      ]
      Author's profile photo kratika varshney
      kratika varshney

      yes you are right something like this

      [{
          "Prod_Id": "001",
          "cost": "<total of all of the 001 entires>"
        },{
          "Prod_Id": "002",
          "cost": "<total of all of the 002 entires>"
        },{
          "Prod_Id": "003",
          "cost": "<total of all of the 003 entires>"
        },
      
      ....
      
      ]
      Author's profile photo Daniel Wroblewski
      Daniel Wroblewski
      Blog Post Author

      Not sure if you were just trying to stump me or this is a real question, but if you are serious, can you ask this as a question so then I can answer it there and, if satisfactory, you can mark it as accepted?

      Author's profile photo kratika varshney
      kratika varshney

      This is a real question. I am stuck in this. If you want i will ask this as a question. Thank you so much.

      Author's profile photo Daniel Wroblewski
      Daniel Wroblewski
      Blog Post Author

      Thanks for moving the question there. I have replied to the question at https://answers.sap.com/questions/13782180/scripting-syntax-for-sum-of-cost-for-every-unique.html.

      Author's profile photo kratika varshney
      kratika varshney

      Thank you so much. Solution worked. Can you please guide me from where i can learn these scripting techniques. 🙂