Azure SDK for Python - Introducing the new CloudEvent.from_json method to convert messages from Service Bus, Event Hubs, and Storage Queues to CloudEvent

We recently added native CloudEvent support to our Event Grid libraries. I created an app for my Intro to the new Azure SDK Python video and discovered that converting a message from a Service Bus Message to CloudEvent wasn’t super intuitive. It looked like this:

event = CloudEvent.from_dict(
                json.loads(str(msg), object_hook=datetime_parser)

I had to first convert the msg to a string, then call json.loads and pass that an object_hook to parse the dates. It took me a pretty long time and a few discussions with the team to figure that out.

I suggested to the Azure SDK Python team that we have a from_json method takes care of all of that for all the various message types, like Service Bus, Event Hub, Storage Queues.

They ran with my suggestion and just shipped it in azure-core:1.17.0 and azure-eventgrid:4.5.0!

You can find the packages here:, and all the other Azure SDK packages here: Here’s the commit if you are interested in seeing all the changes to support this:

Here’s my new code with the new from_json method:

event = CloudEvent.from_json(msg)

This could be considered a small win, but the way I think about it is that the rest of the Azure development community now doesn’t have to go through the struggle I did when trying to go from one message type to another. And my job is about experiencing pain and doing something about it so the rest of the Azure development community doesn’t have to. So, it’s a win for me and win for you.

Here’s the before and after - love it!

"before CloudEvent.from_json and after"