Dominik_Libner | 2021-02-16 09:18:03 UTC | #1
Hello,
My goal is to obtain the last 'userId' from 'participants' array (where the participant 'purpose' is 'agent' or 'user'. This is the execution JSON:
First I tried to use [(@.length-1)] to filter response (assuming that 'agent' or 'user' are always present at the end) like this: { "translationMap": { "AgentId": "$.conversations[0].participants[(@.length-1)].userId" }, "translationMapDefaults": { "AgentId": "[\"UNKNOWN\"]" }, "successTemplate": "{\"AGENT_ID\":#if(${successTemplateUtils.firstFromArray(${AgentId}).length()}>0)${successTemplateUtils.firstFromArray(${AgentId})}#{else}\"UNKNOWN\"#end}" }
In this case translationMap returns no values "[ ]". Why? ================================================ Second I tried to return all 'userId' values and filter them using successTemplate VTL #foreach loop and #if statements. Like this: { "translationMap": { "AgentId": "$.conversations[0]participants[?(@.purpose==\"agent\" || @.purpose==\"user\")].userId" }, "translationMapDefaults": {}, "successTemplate": "{\"AGENT_ID\":#if(${successTemplateUtils.firstFromArray(${AgentId}).length()}>0)#foreach( $participant in $AgentId )#if($foreach.count = $AgentId.size())\"$participant\"#end#end#{else}\"UNKNOWN\"#end}" }
In this case editor returns error: " * Template validation failed for 'config.response.successTemplate'. Details: '#foreach' directive is not allowed in templates."
How to obtain the last element in an array? JSONPath expression seems to not allow something like this: $.conversations[0]participants[?(@.purpose==\"agent\" || @.purpose==\"user\")][(@.length-1)].userId ...so I could use it in my 'translationMap'.
Hope somebody can handle this.. :slight_smile: BR, Dominik
Jason_Mathison | 2021-02-16 11:00:22 UTC | #2
Hi Dominik_Libner,
Could you post the execution JSON as a .json file with anything sensitive replaced? It is a lot easier to work with than trying to mock up what you are working on.
--Jason
Dominik_Libner | 2021-02-16 13:05:58 UTC | #3
I am a new user and can not upload anything:( I am trying a personal message with raw JSON but its yelling at me too... How can I send it to you other way?
Jason_Mathison | 2021-02-16 13:08:07 UTC | #4
I didn't notice that you are a brand new user. Once you have been around for a while you will be able to post .json files. For now you can post it to this thread. Put it inside of a pre-formatted block (click on the </> icon).
Dominik_Libner | 2021-02-16 13:36:44 UTC | #5
I removed some irrelevant objects due to character amount limitation:
{
"conversations": [
{
"conversationId": "d659fca0-cf00-4d47-9fd8-27c1694f28e3",
"conversationStart": "2021-02-15T14:58:30.302Z",
"conversationEnd": "2021-02-15T14:59:36.690Z",
"mediaStatsMinConversationMos": 4.875946521759033,
"mediaStatsMinConversationRFactor": 92.16449737548828,
"originatingDirection": "inbound",
"divisionIds": [
"6acc9a04-d041-4052-8ad9-f862f7e8f0e8"
],
"participants": [
{
"participantId": "75c33f90-a02c-4576-a49f-3966efbe4f07",
"participantName": "Miasto",
"purpose": "customer",
"externalContactId": "ce18d2c0-5306-4685-b106-66833270cb6a",
"sessions": [
{
"mediaType": "voice",
"sessionId": "f0654b50-20f1-44e8-b61a-b0dce94df8e0",
"ani": "tel:+48500500500",
"direction": "inbound",
"dnis": "tel:+48225535357",
"sessionDnis": "tel:+48225535357",
"edgeId": "a89505ef-19c2-4b8e-828b-4af04c8842b1",
"remoteNameDisplayable": "Miasto",
"segments": [
{
"segmentStart": "2021-02-15T14:58:30.302Z",
"segmentEnd": "2021-02-15T14:58:30.349Z",
"segmentType": "system",
"conference": false
},
{
"segmentStart": "2021-02-15T14:58:30.349Z",
"segmentEnd": "2021-02-15T14:59:21.557Z",
"queueId": "fced21de-5d53-47fe-8007-624f118eb647",
"disconnectType": "endpoint",
"segmentType": "interact",
"conference": false
}
],
"metrics": [
{
"name": "nConnected",
"value": 1,
"emitDate": "2021-02-15T14:58:30.349Z"
}
],
"mediaEndpointStats": [
{
"codecs": [
"audio/opus"
],
"minMos": 4.8811516761779785,
"minRFactor": 92.38880157470703,
"maxLatencyMs": 38,
"receivedPackets": 514,
"discardedPackets": 1
},
{
"codecs": [
"audio/opus"
],
"minMos": 4.875946521759033,
"minRFactor": 92.16449737548828,
"maxLatencyMs": 39,
"receivedPackets": 668
},
{
"codecs": [
"audio/opus"
],
"minMos": 4.878118515014648,
"minRFactor": 92.25762176513672,
"maxLatencyMs": 39,
"receivedPackets": 41
},
{
"codecs": [
"audio/opus"
],
"minMos": 4.882193088531494,
"minRFactor": 92.43416595458984,
"maxLatencyMs": 32,
"receivedPackets": 1019,
"discardedPackets": 1
},
{
"codecs": [
"audio/opus"
],
"minMos": 4.8765058517456055,
"minRFactor": 92.18841552734375,
"maxLatencyMs": 47,
"receivedPackets": 310,
"discardedPackets": 9
}
],
"recording": true,
"protocolCallId": "b9ae8c60469c9575868d0ab4f871a10e@10.180.98.50",
"provider": "Edge",
"requestedRoutings": [
"Standard"
]
}
]
},
{
"participantId": "272d12b0-058b-4aa2-913c-c262f5968478",
"participantName": "3f9db579-30d6-4aba-b76a-a90fb8ff0d00",
"purpose": "ivr",
"sessions": [
{
"mediaType": "voice",
"sessionId": "f9915d06-5876-481f-a73d-6bd1ced35332",
"ani": "tel:+48500500500",
"direction": "inbound",
"dnis": "tel:+48225535357",
"sessionDnis": "tel:+48225535357",
"edgeId": "a89505ef-19c2-4b8e-828b-4af04c8842b1",
"remoteNameDisplayable": "3f9db579-30d6-4aba-b76a-a90fb8ff0d00",
"peerId": "f0654b50-20f1-44e8-b61a-b0dce94df8e0",
"segments": [
{
"segmentStart": "2021-02-15T14:58:54.010Z",
"segmentEnd": "2021-02-15T14:58:54.014Z",
"segmentType": "system",
"conference": false
},
{
"segmentStart": "2021-02-15T14:58:54.014Z",
"segmentEnd": "2021-02-15T14:58:54.834Z",
"disconnectType": "transfer",
"segmentType": "ivr",
"conference": false
}
],
"metrics": [
{
"name": "nFlow",
"value": 1,
"emitDate": "2021-02-15T14:58:54.658Z"
},
{
"name": "nFlowOutcome",
"value": 1,
"emitDate": "2021-02-15T14:58:54.674Z"
},
{
"name": "tFlowOutcome",
"value": 1,
"emitDate": "2021-02-15T14:58:54.674Z"
},
{
"name": "tIvr",
"value": 820,
"emitDate": "2021-02-15T14:58:54.834Z"
},
{
"name": "tFlow",
"value": 189,
"emitDate": "2021-02-15T14:58:54.847Z"
},
{
"name": "tFlowExit",
"value": 189,
"emitDate": "2021-02-15T14:58:54.847Z"
}
],
"flow": {
"flowId": "3f9db579-30d6-4aba-b76a-a90fb8ff0d00",
"flowName": "PLSALONTRANSFER",
"flowVersion": "1.0",
"flowType": "INBOUNDCALL",
"exitReason": "TRANSFER",
"entryReason": "tel:+48225535357",
"entryType": "dnis",
"transferType": "ACD",
"transferTargetName": "PL_Inne",
"transferTargetAddress": "7706b257-26ed-441c-a2b3-ea900bf723e0",
"issuedCallback": false,
"startingLanguage": "pl-pl",
"endingLanguage": "pl-pl",
"outcomes": [
{
"flowOutcomeId": "113b73d5-d85b-494c-85fd-c33c51d12798",
"flowOutcomeValue": "SUCCESS",
"flowOutcome": "113b73d5-d85b-494c-85fd-c33c51d12798:SUCCESS",
"flowOutcomeStartTimestamp": "2021-02-15T14:58:54.674Z",
"flowOutcomeEndTimestamp": "2021-02-15T14:58:54.674Z"
}
]
},
"protocolCallId": "ca887afc-fdd9-4386-a536-6226680410d6",
"provider": "Edge",
"remote": "Miasto"
}
]
},
{
"participantId": "cf6127ce-da7f-4a6d-a8e3-b12a55ee6401",
"participantName": "PL_Inne",
"purpose": "acd",
"sessions": [
{
"mediaType": "voice",
"sessionId": "3fb27d39-7523-40d8-a28a-66bfdb14fe5b",
"ani": "tel:+48500500500",
"direction": "inbound",
"dnis": "tel:+48225535357",
"sessionDnis": "tel:+48225535357",
"edgeId": "a89505ef-19c2-4b8e-828b-4af04c8842b1",
"remoteNameDisplayable": "PL_Inne",
"peerId": "f0654b50-20f1-44e8-b61a-b0dce94df8e0",
"segments": [
{
"segmentStart": "2021-02-15T14:58:54.870Z",
"segmentEnd": "2021-02-15T14:58:54.878Z",
"queueId": "7706b257-26ed-441c-a2b3-ea900bf723e0",
"segmentType": "delay",
"conference": false
},
{
"segmentStart": "2021-02-15T14:58:54.878Z",
"segmentEnd": "2021-02-15T14:59:15.257Z",
"queueId": "7706b257-26ed-441c-a2b3-ea900bf723e0",
"disconnectType": "transfer",
"segmentType": "interact",
"sipResponseCodes": [
410
],
"conference": false
}
],
"metrics": [
{
"name": "nOffered",
"value": 1,
"emitDate": "2021-02-15T14:58:54.878Z"
},
{
"name": "tAcd",
"value": 20379,
"emitDate": "2021-02-15T14:59:15.257Z"
}
],
"protocolCallId": "d43ee2f7-7c1c-4ad4-8c1a-a8e9f8c5f520",
"provider": "Edge",
"remote": "Miasto",
"requestedRoutings": [
"Standard"
],
"usedRouting": "Standard",
"selectedAgentId": "c9ebdfc2-5201-451d-84d1-2baabe70fa4f"
}
]
},
{
"participantId": "e594826d-347e-411a-bde6-74ad9d10b05b",
"userId": "70e79652-5482-4858-a0d8-ab3567d924c4",
"purpose": "agent",
"sessions": [
{
"mediaType": "voice",
"sessionId": "247df16e-5a25-4c78-83b5-f12c4ad68271",
"ani": "tel:+48500500500",
"direction": "inbound",
"dnis": "tel:+48225535357",
"sessionDnis": "sip:600fe994f99c6d1c504b0777+dimensiondata.orgspan.com;tgrp=65faf686-5e33-4dab-bd54-47684a7b74ac;trunk-context=dimensiondata@localhost",
"edgeId": "a89505ef-19c2-4b8e-828b-4af04c8842b1",
"peerId": "f0654b50-20f1-44e8-b61a-b0dce94df8e0",
"segments": [
{
"segmentStart": "2021-02-15T14:58:55.360Z",
"segmentEnd": "2021-02-15T14:59:10.728Z",
"queueId": "7706b257-26ed-441c-a2b3-ea900bf723e0",
"disconnectType": "client",
"segmentType": "alert",
"conference": false
}
],
"metrics": [
{
"name": "tAlert",
"value": 15368,
"emitDate": "2021-02-15T14:59:10.728Z"
},
{
"name": "tNotResponding",
"value": 15368,
"emitDate": "2021-02-15T14:59:10.728Z"
}
],
"protocolCallId": "8a73f5b1-1e1a-420a-bb99-19a9d911a699",
"provider": "Edge",
"remote": "Miasto",
"requestedRoutings": [
"Standard"
],
"selectedAgentId": "70e79652-5482-4858-a0d8-ab3567d924c4"
}
]
},
{
"participantId": "04d925ae-db61-4408-8baa-54b623a59a6e",
"userId": "c9ebdfc2-5201-451d-84d1-2baabe70fa4f",
"purpose": "agent",
"sessions": [
{
"mediaType": "voice",
"sessionId": "e80a933a-8177-4953-933f-eef3e07021da",
"ani": "tel:+48500500500",
"direction": "inbound",
"dnis": "tel:+48225535357",
"sessionDnis": "sip:5bf3d0c46bdbb21a31fa8d7b+dimensiondata.orgspan.com;tgrp=65faf686-5e33-4dab-bd54-47684a7b74ac;trunk-context=dimensiondata@localhost",
"edgeId": "a89505ef-19c2-4b8e-828b-4af04c8842b1",
"peerId": "f0654b50-20f1-44e8-b61a-b0dce94df8e0",
"segments": [
{
"segmentStart": "2021-02-15T14:59:12.536Z",
"segmentEnd": "2021-02-15T14:59:15.272Z",
"queueId": "7706b257-26ed-441c-a2b3-ea900bf723e0",
"segmentType": "alert",
"conference": false
},
{
"segmentStart": "2021-02-15T14:59:15.272Z",
"segmentEnd": "2021-02-15T14:59:21.571Z",
"queueId": "7706b257-26ed-441c-a2b3-ea900bf723e0",
"disconnectType": "peer",
"segmentType": "interact",
"conference": false
},
{
"segmentStart": "2021-02-15T14:59:21.690Z",
"segmentEnd": "2021-02-15T14:59:36.690Z",
"queueId": "7706b257-26ed-441c-a2b3-ea900bf723e0",
"wrapUpCode": "7fb334b0-0e9e-11e4-9191-0800200c9a66",
"disconnectType": "peer",
"segmentType": "wrapup",
"conference": false
}
],
"metrics": [
{
"name": "nOverSla",
"value": 1,
"emitDate": "2021-02-15T14:59:15.272Z"
},
{
"name": "tAlert",
"value": 2736,
"emitDate": "2021-02-15T14:59:15.272Z"
},
{
"name": "tAnswered",
"value": 20379,
"emitDate": "2021-02-15T14:59:15.272Z"
},
{
"name": "tTalk",
"value": 6299,
"emitDate": "2021-02-15T14:59:21.571Z"
},
{
"name": "tTalkComplete",
"value": 6299,
"emitDate": "2021-02-15T14:59:21.571Z"
},
{
"name": "nError",
"value": 1,
"emitDate": "2021-02-15T14:59:21.575Z"
},
{
"name": "tAcw",
"value": 15000,
"emitDate": "2021-02-15T14:59:36.690Z"
},
{
"name": "tHandle",
"value": 21299,
"emitDate": "2021-02-15T14:59:36.690Z"
}
],
"mediaEndpointStats": [
{
"codecs": [
"audio/opus"
],
"minMos": 4.876506805419922,
"minRFactor": 92.1884536743164,
"maxLatencyMs": 44,
"receivedPackets": 312,
"discardedPackets": 12
}
],
"protocolCallId": "a128a719-1ec8-4217-a86c-e07249eb1f1d",
"provider": "Edge",
"remote": "Miasto",
"requestedRoutings": [
"Standard"
],
"usedRouting": "Standard",
"selectedAgentId": "c9ebdfc2-5201-451d-84d1-2baabe70fa4f"
}
]
}
]
}
]
}
Jason_Mathison | 2021-02-16 16:27:36 UTC | #6
Using this JSONPath tester: http://jsonpath.herokuapp.com/
Looks like this works: $.conversations[0].participants[-1:].userId
Credit belongs to stack overflow: https://stackoverflow.com/questions/32153985/how-to-find-last-element-in-array-list-using-jsonpath-expression
I just noticed that if you use -1 instead of -1: it will return the value without the array wrapper, letting you get rid of the firstFromArray in the successTemplate!
--Jason
Dominik_Libner | 2021-02-16 15:06:27 UTC | #7
That is great! Thank you. This answer actually closes the topic.
I will go a little off and ask you what if I have to filter object first and take the last element after? Like this: $.conversations[0]participants[?(@.purpose=='agent' || @.purpose=='user')][-1:].userId
Unfortunately this kind of "indexing" is not allowed in JSONPath standard: https://github.com/json-path/JsonPath/issues/272
Does it mean the API response structure must be ordered in a specific manner according to my request? And PureCloud response configuration allows to either just filter objects or just take first or last element from structure? Not both.
Jerome.Saint-Marc | 2021-02-16 15:45:38 UTC | #8
Hello Dominik,
I'll just reply on behalf of Jason as he gave me the information a while ago. :-) We are using the JayWay version of JSONPath. So we can only do what this version supports.
A question to you, are you invoking this Data Action from an Architect flow or from a Script? Just in case, as I had a way around this using Architect.
If you are invoking the Data Action from an Architect flow, you could retrieve an array of UserIDs (array of strings) - $.conversations[0].participants[?(@.purpose=='agent' || @.purpose=='user').userId Then, in your Architect flow, using an expression like this: GetAt(Flow.MyUserIds, Count(Flow.MyUserIds) - 1) (from this [post](https://developer.mypurecloud.com/forum/t/lastcalledagent-routing/7602/10))
Regards,
Jason_Mathison | 2021-02-16 21:09:22 UTC | #9
I agree with Jerome's solution to this. Once you are down to an array of strings for the UserIds, Architect or Scripts should be able to do the final work for you.
Unfortunately the results from the translationMap are always a string, even if the results were actually an array. This limitation makes it hard to do additional list/array processing in the data action. You could probably do some tricky string manipulation if it was really important, but I wouldn't do that unless you had to.
--Jason
ChaseGrigsby | 2021-02-19 11:12:29 UTC | #10
quote="Jason_Mathison, post:6, topic:9999"] Using this JSONPath tester: http://jsonpath.herokuapp.com/[employee monitoring
Looks like this works: $.conversations[0].participants[-1:].userId
Credit belongs to stack overflow: [/quote]
Thanks a lot! Does the PureCloud configuration allow you to simply filter objects separately?
Jason_Mathison | 2021-02-19 14:17:09 UTC | #11
Hi ChaseGrigsby,
I'm sorry, I am not sure what you are asking. Can you give me an example?
--Jason
system | 2021-03-22 14:17:10 UTC | #12
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
This post was migrated from the old Developer Forum.
ref: 9999