PowerShell Azure Function receives webhook data as dictionary instead of JSON string

I’m working on a PowerShell-based Azure Function that processes webhooks from an e-commerce platform after customers complete their orders. During local development, everything worked perfectly when I tested with sample JSON data using tools like Postman.

Here’s the sample JSON structure I was working with:

{
  "order_id": 945612387451230000,
  "customer_email": "[email protected]",
  "status": "completed",
  "order_date": "2021-03-15T14:22:18+00:00",
  "last_modified": "2021-03-15T14:22:18+00:00",
  "order_number": 567,
  "notes": null
}

Locally, I could parse this data easily:

$ParsedData = $Request.Body | ConvertFrom-JSON
$CustomerEmail = $ParsedData.customer_email

However, when the actual webhook hits my deployed function, the JSON conversion fails with this error:

WARNING: Conversion from JSON failed with error: Unexpected character encountered while parsing value: S. Path '', line 0, position 0.

When I output the raw request body, I see:

INFORMATION: System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry

It seems like the webhook data is already parsed into a dictionary object rather than being received as a raw JSON string. My function starts like this:

param($Request, $TriggerMetadata)
try {
    $ErrorActionPreference = "Stop"
    Connect-AzAccount -Identity
    Write-host $Request.Body

What am I missing here? Why does the live webhook behave differently than my local testing setup?

This happens all the time with Azure Functions. When you test locally, the HTTP trigger gets raw JSON strings, but Azure’s runtime automatically deserializes certain content types into PowerShell objects before your function runs.

Your webhook is probably sending data with a content-type header that triggers automatic parsing. Don’t try converting an already-parsed dictionary back to JSON - just work with the dictionary directly:

$CustomerEmail = $Request.Body.customer_email
$OrderId = $Request.Body.order_id

I’ve hit this same issue building webhook handlers for payment processors. Check the object type first and handle both cases:

if ($Request.Body -is [string]) {
    $ParsedData = $Request.Body | ConvertFrom-JSON
} else {
    $ParsedData = $Request.Body
}

This makes your function work in both local dev and production.

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