简体   繁体   中英

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

  • Using Microsoft Flow, send an HTTP request (POST) to an Azure Function w/ "ugly", serialized JSON file in body
  • Azure Function reads file in (then uses ConvertToJson cmdlet to prettify?) and outputs the file back to Flow

Questions :

  1. What do I put in the run.ps1 area of the Azure Function to make this happen? 在此处输入图片说明
  2. What do I put in the functions.json area of the Azure Function to make this happen? 在此处输入图片说明

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.

在此处输入图片说明

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.

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
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM