Websocket Notification event json body

Hello,

I was given the following event as a sample from another team... but this json is an invalid format (has single quotes) am i missing something ? schema here topics shows them up in double quotes.

{'topicName': 'v2.analytics.queues.xxxxxxxxxxxxxxxxx.observations', 'version': '2', 'eventBody': {'group': {'queueId': 'xxxxxxxxxxxxxxxxxxxxxxxxxxx', 'mediaType': 'voice'}, 'data': [{'interval': '2021-08-03T12:33:04.464Z/2021-08-03T12:33:04.464Z', 'metrics': [{'metric': 'oWaiting', 'stats': {'count': 0.0}}, {'metric': 'oInteracting', 'stats': {'count': 1.0}}]}]}, 'metadata': {'CorrelationId': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'}}

Thanks
Ashish

JSON objects always use double quotes. What you have is not a valid JSON object.

Yes exactly, so something must have gone wrong in between.
Thank you @tim.smith

Sorry @tim.smith, I am at a loss.

How is it possible that the events we are getting for the following topic or others were returned with single quotes? We are not doing anything, receiving as it is.

Example:
{'topicName': 'v2.analytics.queues.e25798c8-0d7d-4b44-a6a3-574511c18f0d.observations', 'version': '2', 'eventBody': {'group': {'queueId': 'e25798c8-0d7d-4b44-a6a3-574511c18f0d', 'mediaType': 'voice'}, 'data': [{'interval': '2021-08-03T12:33:04.464Z/2021-08-03T12:33:04.464Z', 'metrics': [{'metric': 'oWaiting', 'stats': {'count': 0.0}}, {'metric': 'oInteracting', 'stats': {'count': 1.0}}]}]}, 'metadata': {'CorrelationId': 'fc46368d-91e8-4c20-b358-b787b480b4e7'}}

Due to this I cannot post it using postman nor I can use it in Azure function as this is not a valid json.

Thanks
Ashish

Where are you getting that from? Whatever is providing that is probably what's responsible for malforming the JSON. Try using a different language or websocket library to verify that you get valid JSON there and the issue exists with your code.

Python SDK has been used.

The following example was used

real-time-queue-observation-query

This is due to Python.
This is because you are not using JSON encoder/decoder properly for the variable that you are printing.

Taking the listen_to_Websocket as an example (from the tutorial you are referencing above):

async def listen_to_Websocket():
    """ Open a new web socket using the connect Uri of the channel """
    async with websockets.connect(new_channel.connect_uri) as websocket:
        print("Listening to websocket")
        """ Message received """
        async for message in websocket:
            message = json.loads(message)
            print("This will print with single quotes:")
            print(message)
            print("This will print with double quotes:")
            print(json.dumps(message))

See JSON encoder and decoder for examples showing how json.loads and json.dumps work in Python.

Regards,

1 Like

Sure I see what you are saying.
I know how json.loads and json.dumps work, someone else was providing data to us.

Thanks for confirming!

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