How to parse nested EventGrid message?

I'm just learning about Azure Functions triggered by a Queue Storage event handler. In this case, Queue Storage is handling Event Grid messages.

Question: How do I use Python to access the various values nested in "body" below?

  • Such as the value of body/data/blobUrl

The Queue Storage message looks like this (indented for readibility):

  • "body" is the nested EventGrid message
{
    "id": "<big-long-guid>", 
    "body": "{
        \"topic\":\"/subscriptions/<big-long-guid>/resourceGroups/azureStorage/providers/Microsoft.Storage/storageAccounts/stgcool\",
        \"subject\":\"/blobServices/default/containers/cont-pics/blobs/profile_pic.jpg\",
        \"eventType\":\"Microsoft.Storage.BlobCreated\",
        \"id\":\"<big-long-guid>\",
        \"data\":{
            \"api\":\"PutBlob\",
            \"clientRequestId\":\"<big-long-guid>\",
            \"requestId\":\"<big-long-guid>\",
            \"eTag\":\"0x8D94CE0B2F5CD71\",
            \"contentType\":\"image/jpeg\",
            \"contentLength\":35799,
            \"blobType\":\"BlockBlob\",
            \"blobUrl\":\"https://stgcool.blob.core.windows.net/cont-pics/profile_pic.jpg\",
            \"url\":\"https://stgcool.blob.core.windows.net/cont-pics/profile_pic.jpg\",
            \"sequencer\":\"00000000000000000000000000003730000000000000312a\",
            \"storageDiagnostics\":{
                \"batchId\":\"<big-long-guid>\"
            }
        },
        \"dataVersion\":\"\",
        \"metadataVersion\":\"1\",
        \"eventTime\":\"2021-07-22T07:17:00.8479184Z\"
    }", 
    "expiration_time": "2021-07-30T05:10:37+00:00", 
    "insertion_time": "2021-07-23T05:10:37+00:00", 
    "time_next_visible": "2021-07-23T05:20:37+00:00", 
    "pop_receipt": "cOQ8m5lN2QgBAAAA", 
    "dequeue_count": 1
}

Here is the sample Function code that generates the above log:

import logging
import json

import azure.functions as func

def main(msg: func.QueueMessage):
    logging.info('Python queue trigger function processed a queue item.')

    result = json.dumps({
        'id': msg.id,
        'body': msg.get_body().decode('utf-8'),
        'expiration_time': (msg.expiration_time.isoformat()
                            if msg.expiration_time else None),
        'insertion_time': (msg.insertion_time.isoformat()
                           if msg.insertion_time else None),
        'time_next_visible': (msg.time_next_visible.isoformat()
                              if msg.time_next_visible else None),
        'pop_receipt': msg.pop_receipt,
        'dequeue_count': msg.dequeue_count
    })

    logging.info(result)

Tried:

  • Wrapping msg.get_body() in different iterations of get_json() and json.dumps() but received errors.


from Recent Questions - Stack Overflow https://ift.tt/2V68RX8
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation