ericOnline
ericOnline

Reputation: 1977

JSON Prettifier Using Azure Function w/ PowerShell and HTTP Trigger

Thought this would be pretty simple, but alas, I can't figure it out. It appears that PowerShell will prettify JSON with a single cmdlet.

Goal: Prettify JSON using a PowerShell Azure Function app

Questions:

  1. What do I put in the run.ps1 area of the Azure Function to make this happen? enter image description here
  2. What do I put in the functions.json area of the Azure Function to make this happen? enter image description here

Upvotes: 0

Views: 998

Answers (2)

Anatoli Beliaev
Anatoli Beliaev

Reputation: 1664

Are you looking for something like this?

using namespace System.Net

param($Request, $TriggerMetadata)

Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $Request.RawBody | ConvertFrom-Json | ConvertTo-Json
})

Upvotes: 1

Mohit Verma
Mohit Verma

Reputation: 5294

I have taken below serialize string

'{ "baz": "quuz", "cow": [ "moo", "cud" ], "foo": "bar" }'

which was mentioned in Prettify json in powershell 3

Here is my function which i used with HttpPost and send the request:

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."

# Interact with query parameters or the body of the request.
$name = $Request.Query.baz
if (-not $name) {
    $name = $Request.Body.baz
}

if ($name) {
    $status = [HttpStatusCode]::OK
    $body = "Hello $name"
}
else {
    $status = [HttpStatusCode]::BadRequest
    $body = "Please pass a name on the query string or in the request body."
}

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

and below you can see , i am able to read it from the string which i posted.

enter image description here

You can use ConvertFrom-Json to convert it but i wondering if you even need it as you can access it by doing below:

$name = $Request.Query.baz

my binding is same as yours. Hope it helps.

Let me know if you still need any help.

Upvotes: 1

Related Questions