Cannot access items within JSON response

I am using Conversation aggregates query to pull conversation IDs. The query is executed successfully and gives me response. However, I am only able to print the entire response. I am unable to access only the conversation ID field:

api_response: PureCloudPlatformClientV2.ConversationAggregationQuery = api_instance.post_analytics_conversations_aggregates_query(query)
items: PureCloudPlatformClientV2.Conversation
response_list = api_response
pprint(response_list)
sourceFile.write(str(api_response))

Sample response:

{'results': [{'data': [{'interval': '2021-12-31T18:30:00.000Z/2022-01-01T18:30:00.000Z',

                    'metrics': [{'metric': 'nConnected',

                                 'qualifier': None,

                                 'stats': {'count': 1,

                                           'count_negative': None,

                                           'count_positive': None,

                                           'current': None,

                                           'denominator': None,

                                           'max': None,

                                           'min': None,

                                           'numerator': None,

                                           'ratio': None,

                                           'sum': None,

                                           'target': None}},

                                {'metric': 'nOffered',

                                 'qualifier': None,

                                 'stats': {'count': 1,

                                           'count_negative': None,

                                           'count_positive': None,

                                           'current': None,

                                           'denominator': None,

                                           'max': None,

                                           'min': None,

                                           'numerator': None,

                                           'ratio': None,

                                           'sum': None,

                                           'target': None}}],

                    'views': None}],

          'group': {'conversationId': '00212cfb-eb97-4dc9-ae98-2e88aab9d2de',

                    'mediaType': 'email'}},

         {'data': [{'interval': '2021-12-31T18:30:00.000Z/2022-01-01T18:30:00.000Z',

                    'metrics': [{'metric': 'nConnected',

                                 'qualifier': None,

                                 'stats': {'count': 1,

                                           'count_negative': None,

                                           'count_positive': None,

                                           'current': None,

                                           'denominator': None,

                                           'max': None,

                                           'min': None,

                                           'numerator': None,

                                           'ratio': None,

                                           'sum': None,

                                           'target': None}},

                                {'metric': 'nOffered',

                                 'qualifier': None,

                                 'stats': {'count': 1,

                                           'count_negative': None,

                                           'count_positive': None,

                                           'current': None,

                                           'denominator': None,

                                           'max': None,

                                           'min': None,

                                           'numerator': None,

                                           'ratio': None,

                                           'sum': None,

                                           'target': None}}],

                    'views': None}],

          'group': {'conversationId': '0024b87f-657e-4ea2-bbfb-1c58865133e7',

                    'mediaType': 'email'}},

         {'data': [{'interval': '2021-12-31T18:30:00.000Z/2022-01-01T18:30:00.000Z',

                    'metrics': [{'metric': 'nConnected',

                                 'qualifier': None,

                                 'stats': {'count': 1,

                                           'count_negative': None,

                                           'count_positive': None,

                                           'current': None,

                                           'denominator': None,

                                           'max': None,

                                           'min': None,

                                           'numerator': None,

                                           'ratio': None,

                                           'sum': None,

                                           'target': None}},

                                {'metric': 'nOffered',

                                 'qualifier': None,

                                 'stats': {'count': 1,

                                           'count_negative': None,

                                           'count_positive': None,

                                           'current': None,

                                           'denominator': None,

                                           'max': None,

                                           'min': None,

                                           'numerator': None,

                                           'ratio': None,

                                           'sum': None,

                                           'target': None}}],

                    'views': None}],

          'group': {'conversationId': '002a9320-1edb-4c21-a637-e73eea305ace',

                    'mediaType': 'email'}},

         {'data': [{'interval': '2021-12-31T18:30:00.000Z/2022-01-01T18:30:00.000Z',

                    'metrics': [{'metric': 'nConnected',

                                 'qualifier': None,

                                 'stats': {'count': 2,

                                           'count_negative': None,

                                           'count_positive': None,

                                           'current': None,

                                           'denominator': None,

                                           'max': None,

                                           'min': None,

                                           'numerator': None,

                                           'ratio': None,

                                           'sum': None,

                                           'target': None}},

                                {'metric': 'nOffered',

                                 'qualifier': None,

                                 'stats': {'count': 1,

                                           'count_negative': None,

                                           'count_positive': None,

                                           'current': None,

                                           'denominator': None,

                                           'max': None,

                                           'min': None,

                                           'numerator': None,

                                           'ratio': None,

                                           'sum': None,

                                           'target': None}}],

                    'views': None}]

Hi,

I tried this out as an example and I am able to access all properties from the api response. Please double check your python syntax for accessing items in a list of objects. I believe you are trying to access the conversationId from the group object in the api_response. I am not an experienced python programmer but something like this may work for your use case:

    for item in api_response.results:
        pprint(item.group["conversationId"])

Thanks,

Mike

Hi Mike,

Thanks a lot for your response. It gives a "Key-error" when using your syntax, tried changing the conversation_id exactly how it is returned from the object:

Thanks,
Somik Mukherjee

Hi,

The reason why you are getting a key error is because the key conversation_id does not exist. The correct key is conversationId as seen in the code example I gave above.

Thanks,

Mike

I tried the same as well the other day but it also came up with key error:

Uploading: Screenshot 2022-07-14 203952.png...

Hi,

can you upload a formatted example of your API response please?

Thanks,

Mike

Hi Mike,

Here is the response:
Created a channel
{'entities': [{'id': 'v2.routing.queues.26b2c827-93b4-490e-b58d-0ba9206e828d.conversations',
'self_uri': None}]}
C:\git\genesys-cloud\python-scripts\scripts_somik\automatic_tranfer_interactions\auto_transfer_interaction.py:121: DeprecationWarning: There is no current event loop
grouped_async = asyncio.gather(listen_to_Websocket())
C:\git\genesys-cloud\python-scripts\scripts_somik\automatic_tranfer_interactions\auto_transfer_interaction.py:122: DeprecationWarning: There is no current event loop
asyncio.get_event_loop().run_until_complete(grouped_async)
Listening to websocket
Display analytics observation query.
Traceback (most recent call last):
File "C:\git\genesys-cloud\python-scripts\scripts_somik\automatic_tranfer_interactions\auto_transfer_interaction.py", line 122, in
asyncio.get_event_loop().run_until_complete(grouped_async)
File "C:\Users\somik.mukherjee\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 646, in run_until_complete
return future.result()
File "C:\git\genesys-cloud\python-scripts\scripts_somik\automatic_tranfer_interactions\auto_transfer_interaction.py", line 110, in listen_to_Websocket
display_queue_observation()
File "C:\git\genesys-cloud\python-scripts\scripts_somik\automatic_tranfer_interactions\auto_transfer_interaction.py", line 95, in display_queue_observation
pprint(item.group["conversationId"])
KeyError: 'conversationId'

Thanks,
Somik

Hi,

I'm not sure why the above code example is not working for you so I am going to post my full example code below which has been tried and tested to work correctly. I suggest that you run this example code separately from your main application to see how it works first, this way you may be able to pin point the syntax errors or where you are going wrong in your main application.

import PureCloudPlatformClientV2
from PureCloudPlatformClientV2.rest import ApiException
from pprint import pprint

PureCloudPlatformClientV2.configuration.access_token = 'your_access_token_goes_here'

api_instance = PureCloudPlatformClientV2.ConversationsApi()

body = {
  "interval": "your_interval_goes_here",
  "groupBy": [
    "conversationId"
  ],
  "metrics": [
    "nConnected"
  ],
}

try:
    api_response = api_instance.post_analytics_conversations_aggregates_query(body)
    
    for item in api_response.results:
      pprint(item.group["conversationId"])
      
except ApiException as e:
    print("Exception when calling PostAnalyticsConversationsAggregatesQueryRequest->post_analytics_conversations_aggregates_query: %s\n" % e)

Thanks,

Mike

Hi Mike,

I am using queue observation query and not conversation detail or aggregate query. Below is my body:

query = PureCloudPlatformClientV2.QueueObservationQuery() # QueueObservationQuery | query

query = {

    "filter": {

        "type": "AND",

        "clauses": [

            {

                "type": "AND",

                        "predicates":[

                            {

                                "dimension" : "queueId",

                                "value": QUEUE_ID

                            },

                            {

                                "dimension": "mediaType",

                                "value": "email"

                            }

                        ]

            }

        ]

        },

    "detailMetrics": ["oWaiting"],

    "metrics":["oWaiting"]

    }

try:

    # Query for queue observations

    api_response = api_instance.post_analytics_queues_observations_query(query)

    print("Display analytics observation query.")

    # pprint(api_response.results)

    for item in api_response:

        pprint(item.group["conversationId"])

    sourceFile.write(str(item.group["conversationId"]))

    sourceFile.close()

       

except ApiException as e:

    print ("Exception when calling AnalyticsApi->post_analytics_queues_observations_query: %s\n" % e)

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