0

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? 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
ericOnline
  • 1,586
  • 1
  • 19
  • 54
  • 2
    GET Methods will discard the body of a request, so that rules out Get. You could use Post or PUT, both are for sending body contents to a web server. Traditionally PUT and PATCH are for update statements, while POST could be used for Update or Create. – FoxDeploy Sep 06 '19 at 18:00
  • Thank you; I'll use POST. Any PowerShell syntax recommendations for actually knocking this out? – SeaDude Sep 08 '19 at 05:29

2 Answers2

1

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.

Mohit Verma
  • 5,140
  • 2
  • 12
  • 27
  • Please note in the OP, I'm POST'ing a file of serialized JSON to the Azure Function rather than a string. Ideally, I'd like to get a prettified JSON file as output in the response. How does this change the PowerShell code? – ericOnline Sep 11 '19 at 20:32
  • still no luck for me. Status 400 Bad Request: "Please pass a name on the query string or in the request body." Again, I'm sending a FILE to the function NOT a JSON string. Any further ideas? – ericOnline Oct 10 '19 at 17:53
1

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
})
Anatoli Beliaev
  • 1,614
  • 11
  • 13