Home > Enterprise >  Convert JSON data from http body request to string in PowerShell
Convert JSON data from http body request to string in PowerShell

Time:10-06

I'm building a Function App in Azure using PowerShell HTTP Trigger. The JSON body content looks something like this:

Input

{
    "name": "azure 0103",
    "time": 1400,
    "stages": {
        "MultiStageReviewSettings": [
            {
                "StageName": "Stage1",
                "Reviewers": [
                    "[email protected]"
                ]
            }
        ]
    }
}
param($Request, $TriggerMetadata)
$label = $Request.Body.Name
$time = $Request.Body.Time
$stages = $Request.Body.Time

I need to covert the stages object to a single line string and it has to be like this:

Output

$stringData = '{ "MultiStageReviewSettings": [ { "StageName": "Stage1", "Reviewers": [ "[email protected]" ] } ] }'

CodePudding user response:

As mentioned by Santiago the way to do it is :

$stages = $Request.Body.stages | ConvertTo-Json -Compress -Depth 99
  • Related