Tip #1456: Enumerate and extract JSON object properties

tl;dr

Use Extract JSON code only connector to select only a subset of object properties matching specified regular expression.

dr;tl

When we were brewing some voodoo with Azure blobs, the returned metadata would be contained in the header prefixed with x-ms-meta-. What on earth an honest hard-working maker is supposed to do with this?

{
    "statusCode": 200,
    "headers": {
        "ETag": "\"0x8DC52D7FC2A70E3\"",
        "Server": "Windows-Azure-Blob/1.0,Microsoft-HTTPAPI/2.0",
        "x-ms-request-id": "2c5b5731-e01e-006e-7cc0-84eaba000000",
        "x-ms-version": "2018-03-28",
        "x-ms-meta-width": "640",
        "x-ms-meta-height": "480",
        "Date": "Tue, 02 Apr 2024 05:45:08 GMT",
        "Content-Length": "0",
        "Last-Modified": "Tue, 02 Apr 2024 05:44:38 GMT"
    }
}

Another Code only Connector (CoC) to the rescue. This one is called Extract JSON, and the method takes three parameters: Input object, Regex to match the object properties, and Replacement expression for the matched properties. It returns a simple object holding only matching properties (with optionally modified names).

A screenshot from Microsoft Power Automate showing a segment of a workflow. The focus is on a 'Code only connector' action titled 'Extract Json', highlighted by a red arrow pointing to it from a 'Get Meta' action above. Within the 'Extract Json' action, the 'Parameters' tab is open, displaying an 'Input' field populated with 'Headers'. Below, two more fields are shown, 'Regex' with the value '^x-ms-meta-' and 'Replace' which is blank, ready for input. This setup indicates that the action is configured to extract JSON data based on specified headers and a regular expression pattern.

And this is the output of the action.

{
    "statusCode": 200,
    "headers": {
       ...
    },
    "body": {
        "Output": {
            "width": "640",
            "height": "480"
        },
        "Properties": [
            "width",
            "height"
        ]
    }
}

Array of all properties is a bonus. If regex is blank, action will match and return all properties; in this scenario the Properties array will contain all properties of the object. No need to bend backwards with xpath and xml. Do what you must with it, I’m sure you have an idea or two up your sleeve already.

Leave a Reply

Your email address will not be published. Required fields are marked *