You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure PowerShell HTTP Trigger函数应用:将JSON对象转为单行字符串

使用PowerShell HTTP Trigger构建Azure函数:将请求体中的stages对象转为单行JSON字符串

输入内容

请求体JSON

{
    "name": "azure 0103",
    "time": 1400,
    "stages": {
        "MultiStageReviewSettings": [
            {
                "StageName": "Stage1",
                "Reviewers": [
                    "ayewrewtela@t7rfdspfdsc.onfdsmicrosoft.com"
                ]
            }
        ]
    }
}

现有PowerShell代码

param($Request, $TriggerMetadata)
$label = $Request.Body.Name
$time = $Request.Body.Time
$stages = $Request.Body.Time

需求说明

需要将请求体中的stages对象转换为单行格式化的JSON字符串,最终目标效果如下:

目标输出

$stringData = '{ "MultiStageReviewSettings": [ { "StageName": "Stage1", "Reviewers": [ "ayewrewtela@t7rfdspfdsc.onfdsmicrosoft.com" ] } ] }'

解决方案

修改PowerShell代码,利用ConvertTo-Json的-Compress参数直接将stages对象转为单行JSON字符串:

param($Request, $TriggerMetadata)
$label = $Request.Body.Name
$time = $Request.Body.Time
# 修正取值错误,并转换为单行JSON字符串
$stages = $Request.Body.stages
$stringData = $stages | ConvertTo-Json -Compress

关键说明

  1. 原代码中$stages = $Request.Body.Time是错误取值,需改为$Request.Body.stages以正确获取请求体里的stages对象
  2. ConvertTo-Json -Compress参数会自动将JSON结构压缩为单行格式,同时保留合法的JSON语法,无需手动拼接字符串

内容的提问来源于stack exchange,提问作者Ayoub

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 06:15:22