cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2078
Views
3
Helpful
7
Replies

How to get a value from a JSON array in UCCX?

tomc101010
Level 1
Level 1

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:

tomc101010_0-1679311799800.png

 

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

1 Accepted Solution

Accepted Solutions

Anthony Holloway
Cisco Employee
Cisco Employee

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 indefiniteif it contains:

  • .. - a deep scan operator
  • ?(<expression>) - an expression
  • [<number>, <number> (, <number>)] - multiple array indexes

Indefinite 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.

Screenshot 2023-03-20 at 9.09.01 PM.png

View solution in original post

7 Replies 7

b.winter
VIP
VIP

Have you tried:
$.hits.hits[0]._source.customerAttributes[1].attributeId

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 

 

 

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"?

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.

Anthony Holloway
Cisco Employee
Cisco Employee

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 indefiniteif it contains:

  • .. - a deep scan operator
  • ?(<expression>) - an expression
  • [<number>, <number> (, <number>)] - multiple array indexes

Indefinite 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.

Screenshot 2023-03-20 at 9.09.01 PM.png

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

Happy to help!