How to get an azure function app using powershell to loop through JSON

Johnny B Bad 20 Reputation points
2024-04-30T13:45:00.94+00:00

I am having trouble getting my azure function app to work. The powershell works OK locally, but not in the function app.

I am trying to loop through the JSON and remove SYSTEMCOLUMNS_ and then I want to return $data back as JSON

Input

{
  "SYSTEMCOLUMN_1": "2024-04-30T10:57:54.2868986Z",
  "Process": "Testing",
  "Surname": "Bade",
  "SYSTEMCOLUMN_2": true,
  "SYSTEMCOLUMN_3": "",
  "Firstname": "Johnny" 
}

User's image

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# Convert the request body from JSON to a PowerShell object
$data = $Request.Body
# Loop through the properties of the JSON data and remove keys starting with "SYSTEMCOLUMN_"
foreach ($key in $data.PSObject.Properties.Name) 
{
    if ($key -like "SYSTEMCOLUMN_*") 
	{
        $data.PSObject.Properties.Remove($key)
    }
}

# Convert the modified data back to JSON
$updatedJson = $data

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $updatedJson
})


It just returns the original data back, and isn't managing to strip the SYSTEMCOLUMNS_ out

User's image

The powershell code works OK locally in VScode. I guess that my object in the Function App is behaving differently

Local Output

{

"Process": "Testing",

"Surname": "Bade",

"Firstname": "Johnny"

}

Any ideas how to solve this?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,350 questions
0 comments No comments
{count} votes

Accepted answer
  1. SwathiDhanwada-MSFT 17,881 Reputation points
    2024-05-01T06:35:46.7033333+00:00

    @Johnny B Bad Thanks for reaching out. I have reproduced your issue and I have noticed similar issue, so I modified provided code as when you are converting request body to PowerShell Object, its being stored as Ordered Hash table in Azure functions editor. Accordingly, I have modified the code.

    Here is code and respective output for your reference.

    using namespace System.Net
    # Input bindings are passed in via param block.
    param($Request, $TriggerMetadata)
    
    # Write to the Azure Functions log stream.
    Write-Host "PowerShell HTTP trigger function processed a request."
    
    # Convert the request body from JSON to a PowerShell object
    $data = $Request.Body  
    
    $cloneData = $data.Clone()
    # Loop through the properties of the JSON data and remove keys starting with "SYSTEMCOLUMN_"
    
    $data.GetEnumerator() | ForEach-Object{
        if ($_.key -like "SYSTEMCOLUMN_*") 
        {
            $cloneData.remove($_.key)
        }
    }
    
    # Convert the modified data back to JSON
    $updatedJson = $cloneData | ConvertTo-Json -Depth 100
    
    # Associate values to output bindings by calling 'Push-OutputBinding'.
    Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
        StatusCode = [HttpStatusCode]::OK
        Body = $updatedJson
    })
    

    User's image

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful