cos | 2022-06-01 18:07:16 UTC | #1
I'm working with the the Conversations Aggregate Query, I am able to get the response back easily enough but the result is a severely nested JSON response. Is there a built in method within the SDK to parse this or any pointers on being able to normalize it into a pandas dataframe? The goal would be to export this as a CSV in a format that can be easily used for analyzing (for now, future use is to load it into a db).
The code I am using is below:
import PureCloudPlatformClientV2 from PureCloudPlatformClientV2.rest import ApiException from pprint import pprint
CLIENTID = 'ID' CLIENTSECRET = 'sec' ORGREGION = 'uswest_2'
Set environment
region = PureCloudPlatformClientV2.PureCloudRegionHosts[ORGREGION] PureCloudPlatformClientV2.configuration.host = region.getapi_host()
OAuth when using Client Credentials
apiclient = PureCloudPlatformClientV2.apiclient.ApiClient() \ .getclientcredentialstoken(CLIENTID, CLIENTSECRET) token = apiclient.access_token
Configure OAuth2 access token for authorization: PureCloud OAuth
PureCloudPlatformClientV2.configuration.access_token = token
create an instance of the API class
apiinstance = PureCloudPlatformClientV2.ConversationsApi() body = PureCloudPlatformClientV2.ConversationAggregationQuery() # ConversationAggregationQuery | query body.interval = '2022-05-30T00:00:00.000Z/2022-05-31T00:00:00.000Z' body.metrics = ["nOffered","tTalkComplete"] body.groupby = ['userId']
try:
Query for conversation aggregates
apiresponse = apiinstance.postanalyticsconversationsaggregatesquery(body).tojson() print(apiresponse) except ApiException as e: print("Exception when calling PostAnalyticsConversationsAggregatesQueryRequest->postanalyticsconversationsaggregatesquery: %s\n" % e)
John_Carnell | 2022-06-01 19:49:45 UTC | #2
Hi COS,
Our Python SDK does not have any built-in mechanisms to convert the results of an API call like the API query into a Pandas data frame. Since our API enforces the API contracts one of the more common mistakes people make is that they try to use the pd.json_normalize function to try and flatten the object into a Pandas data frame. This code fails because you are not getting back a dictionary of dictionaries-style data structure that Pandas expect.
We do have a function in the util.py called sanitizeforserialization() that can help take a result and make it more parseable for JSON libraries. That being said, I am afraid right now the only option you have is perform your query and then manually copy the data into a data structure that can be then consumed by a Pandas data structure.
Thanks, John Carnell Manager, Developer Engagement
cos | 2022-06-01 20:10:59 UTC | #3
Thanks for the reply John! Within the code above, would you be able to provide an example of how I would add the sanitize function? I should clarify too it doesn't have to be a dataframe within pandas specifically I just need something more so tabular to work with. I assume the answer is still the same, just figured i'd clarify.
John_Carnell | 2022-06-01 20:19:53 UTC | #4
Hi Cos,
I don't have anything readily available (it's been forever since I did some Pandas/Juypter notebook work) :).
I did write up a ticket for a blueprint from our team that will demonstrate this. I know this wont help you in the short term, but it might help answer this question in the future. I am going to send this thread onto some of my colleagues on the partner side and see if they can come up with something, but I can't promise anything in a timely manner.
Thanks, John Carnell Manager, Developer Engagement
system | 2022-07-02 20:19:55 UTC | #5
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: 14967