Can't sum array of numbers within successTemplate due to translationMap resolving array to string

Hey there, everyone!

I'm trying to get an aggregate count of all agents in a queue utilizing the /api/v2/analytics/queues/observations/query endpoint. Our response is turning out like this:

{
  "systemToOrganizationMappings": {
    "AWAY": [
      "5e5c5c66-ea97-4e7f-ac41-6424784829f2"
    ],
    "OFFLINE": [
      "ccf3c10a-aa2c-4845-8e8d-f59fa48c58e5"
    ]
  },
  "results": [
    {
      "group": {
        "queueId": "18e5d10d-df18-4234-8ebd-1fa2d5a95695"
      },
      "data": [
        {
          "metric": "oOffQueueUsers",
          "qualifier": "5e5c5c66-ea97-4e7f-ac41-6424784829f2",
          "stats": {
            "count": 1
          }
        },
        {
          "metric": "oOffQueueUsers",
          "qualifier": "ccf3c10a-aa2c-4845-8e8d-f59fa48c58e5",
          "stats": {
            "count": 34
          }
        }
      ]
    }
  ]
}

So far so good. Our response configuration is where I'm hitting a snag. It currently looks like this:

{
  "translationMap": {
    "logged_in_agents": "$.results[0].data[?(@.qualifier IN [ '5e5c5c66-ea97-4e7f-ac41-6424784829f2','31fe3bac-dea6-44b7-bed7-47f91660a1a0','3fd96123-badb-4f69-bc03-1b1ccc6d8014','d2390a99-8546-bad9-8f0a-219548e8aeb0','bbdff279-7ae1-48ea-bade-7831d7234c64','227b37e2-f1d0-4dd0-8f50-badd7cf6d158'])].stats.count"
  },
  "translationMapDefaults": {
    "logged_in_agents": "[ 0 ]"
  },
  "successTemplate": "{\"logged_in_agents\": ${logged_in_agents} }"
}

I'm essentially getting the count of all status's that are in the qualifier filter. So, the returned response is this, which I'm are trying to sum together:

{
    "logged_in_agents": "[ 3, 34, 21, 7 ]"
}

However, when utilizing the .sum() function of JSONPath, I'm getting an error because it's a string, not an array. Utilizing $esc.jsonDecode() doesn't help either. It appears that it only is converted to an array during the output transformation phase, which is after the success template phase.

Is it at all possible to add these numbers together using Velocity or JSONPath? My goal is to not have to write out a translation map item for every single possibility or handle it within Architect. I saw another post that was similar but it utilized two translationMap items and then used velocity macros to do the math on them. Keeping this clean is the goal, so if there's a way to do that, I'm all ears. Thanks!

Late so quick reply.
This is how I did it. Basically I think everything gets treated as string in template, which is a shame.
This strips the json and splits on the comma to get a total.

Adjust to suit yours, should work.

{
  "translationMap": {
    "statuses": "$.results[0].data..qualifier",
    "counts": "$.results[0].data..stats.count"
  },
  "translationMapDefaults": {
    "statuses": "[\"Error\"]",
    "counts": "[0]"
  },
  "successTemplate": " #set ($countsArray = $counts.replace(\"[\", \"\").replace(\"]\", \"\").replace(\" \", \"\").split(\",\")) {\"counts\": ${counts}, \"statuses\": ${statuses},\"total\":$math.getTotal(${countsArray})}"
}
1 Like

If it works, it works haha. We've been in touch with Genesys Professional Services as well for their input, but I'll pass this along to see if it's something we want to mess with. I agree, it is a shame templates treat everything as strings. At minimum it'd be nice to see the JSON parsing functions be able to unwrap strings for further processing. Thank you, it's greatly appreciated! :slight_smile:

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.