03-20-2023 04:42 AM
Hello,
I need your help regarding obtaining some values from a JSON array in UCCX?
Here is a JSON file example:
{
"took": 252,
"timed_out": false,
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"hits": [
{
"_source": {
"customerAttributes": [
{
"attributeId": 37,
"value": "0.000"
},
{
"attributeId": 6,
"value": "20"
}
],
"customerInteractions": []
}
}
]
}
}
I need to obtain the “value” for the "attributeId": 6.
The following JSON paths returns me the correct result in http://jsonpath.com/
$..customerAttributes[?(@.attributeId==6)].value
$.hits.hits[0]._source.customerAttributes[?(@.attributeId==6)].value
In the UCCX script I get the following message:
I know that UCCX uses the jayway implementation of JSONPath (from the previous post: @Anthony Holloway
https://community.cisco.com/t5/contact-center/get-the-length-of-a-json-array-in-ccx/m-p/3907604/highlight/true#M112496), however link for the test is not available.
I think the error is inside the filter “[?(@.attributeId==6)]”, since for the JSON path – “$.hits.hits[0]._source.customerAttributes[1].value”, the UCCX returns me the value "20".
Any idea?
Thanks in advance.
Best regards,
Tom C
Solved! Go to Solution.
03-20-2023 06:12 PM
Your JSONPath looks right, but the error you got gives a clue that you needed an Array, instead of a String to stuff the results in.
This is talked about in the JSONPath documentation here:
When evaluating a path you need to understand the concept of when a path is definite
. A path is indefinite
if it contains:
..
- a deep scan operator?(<expression>)
- an expression[<number>, <number> (, <number>)]
- multiple array indexesIndefinite
paths always returns a list.
Source: https://github.com/json-path/JsonPath
Now that we know that, we can use a String Array to stuff the search results into, and then if the result set length is 1, we have our match.
03-20-2023 05:15 AM
Have you tried:
$.hits.hits[0]._source.customerAttributes[1].attributeId
03-20-2023 06:22 AM
Hello B.Winter,
when I specify the exact position in an array it works fine.
So, for the JSON path "$.hits.hits[0]._source.customerAttributes[1].attributeId" UCCX returns the value for the attribute ID. That is OK.
But what to do when we don't know the exact position in the array? We just know attribudeIDs and need to read values for the attributes. That's why we try to use something like this [?(@.attributeId==6)], but unfortunately UCCX doesn't recognize it.
Best regards,
Tom C
03-20-2023 06:35 AM
But based on which "criteria" are you deciding the "correct" element of an array then? How do you determine, if it's element 1 or 2 or 10, ... if the value you are looking for isn't always in the array element 1 for example.
What are you trying to achieve with this? Do you only want to check, if the data includes an "attributeId" with value "6"?
Or do you want to read the current value of the "attributeId"?
03-20-2023 07:04 AM
A JSON file contains pairs - attributeID, value. I just know the attributeID (not the element position in array) and want to read the values for that attribute. That's why I need some kind of filter where attribueID is going to be defined.
03-20-2023 06:12 PM
Your JSONPath looks right, but the error you got gives a clue that you needed an Array, instead of a String to stuff the results in.
This is talked about in the JSONPath documentation here:
When evaluating a path you need to understand the concept of when a path is definite
. A path is indefinite
if it contains:
..
- a deep scan operator?(<expression>)
- an expression[<number>, <number> (, <number>)]
- multiple array indexesIndefinite
paths always returns a list.
Source: https://github.com/json-path/JsonPath
Now that we know that, we can use a String Array to stuff the search results into, and then if the result set length is 1, we have our match.
03-22-2023 08:09 AM
Thank you, Anthony. That is exactly what I was looking for.
I have to test this on a bigger JSON file with a different kind of data, so we will see how it will go.
BR,
Tom C
03-22-2023 08:40 AM
Happy to help!
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide