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

PowerShell中如何格式化Invoke-RestMethod返回的REST响应结构

问题背景

我正在调用一个REST服务,使用如下PowerShell代码:

$response = Invoke-RestMethod -Uri $URL -Headers $headers -Method POST -Body $body_json -ContentType "application/json" 
$response.Outputs

得到的响应格式为每个字段对应一个值数组,示例如下:

Actual: {
        "2017-08-29T14:37:47.137",
        "2017-08-30T13:07:09.563",
        "2017-08-30T14:41:29.023"
    },
    Start: {
        "2017-08-29T14:36:12.42",
        "2017-08-30T12:59:53.05",
        "2017-08-30T14:40:45.34"
    },
    NumScrapsList: {
        0,
        3,
        ...

我希望将其转换为按索引聚合的对象数组格式,示例如下:

{   
        "NumScrapsList":0,
        "Actual":"2017-08-29T14:37:47.137",
        "Start":"08-29T14:36:12.42"
    },
    {
        "NumScrapsList":3,
        "Actual":"2017-08-30T13:07:09.563",
        "Start":"2017-08-30T12:59:53.05"
    }

使用Python我可以通过以下代码实现(包含"outputs"键):

outputs = [dict(zip(resp['Outputs'].keys(), e))
           for e in zip(*resp['Outputs'].values())]

pprint(outputs)

但我不知道如何在PowerShell中实现该功能,请问有什么可行的实现思路?

补充说明

我通过Invoke-RestMethod获取的$response.outputs完整结构如下:

Type         : {a, b, c}
Code        : {xxx, yyy, eee}
CompletionDate : {1900-01-01T00:00:00, 1900-01-01T00:00:00, 1900-01-01T00:00:00}
OrderQuantity        : {30, 30, 3}
NumScraps            : {0, 0, 0}
ActualDate      : {2021-11-16T15:17:00, 2021-11-16T15:18:00, 1900-01-01T00:00:00}
Status               : {WT, FD, RT}
Order           : {70000, 30794, 94098}
Sequence        : {0300, 0400, 0500}

将其执行ConvertTo-Json后的输出为:

{
    "Type":  [
                         "a",
                         "b",
                         "c"
                     ],
    "Code":  [
                          "xxx",
                          "yyy",
                          "eee"
                      ],
    "CompletionDate":  [
                                 "1900-01-01T00:00:00",
                                 "1900-01-01T00:00:00",
                                 "1900-01-01T00:00:00"
                             ],
    "OrderQuantity":  [
                          30,
                          30,
                          3
                      ],
    "NumScraps":  [
                      0,
                      0,
                      0
                  ],
    "ActualDate":  [
                            "2021-11-16T15:17:00",
                            "2021-11-16T15:18:00",
                            "1900-01-01T00:00:00"
                        ],
    "Status":  [
                   "WT",
                   "FD",
                   "RT"
               ],
    "Order":  [
                       "70000",
                       "30794",
                       "94098"
                   ],
    "Sequence":  [
                          "0300",
                          "0400",
                          "0500"
                      ]
}
解决方案

实现逻辑和你提供的Python代码逻辑完全对齐,不需要硬编码字段名,可适配任意字段数量的返回结构:

# 1. 提取返回结果的所有字段名
$fieldNames = $response.Outputs | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name

# 2. 以第一个字段的数组长度作为总条目数(要求所有字段的数组长度一致)
$itemCount = $response.Outputs.$($fieldNames[0]).Count

# 3. 按索引遍历,聚合生成对象数组
$outputArray = for ($index = 0; $index -lt $itemCount; $index++) {
    $currentItem = [ordered]@{}
    foreach ($field in $fieldNames) {
        $currentItem[$field] = $response.Outputs.$field[$index]
    }
    [PSCustomObject]$currentItem
}

# 验证输出,转为JSON查看格式是否符合要求
$outputArray | ConvertTo-Json

如果偏好更简洁的写法,也可以用管道实现:

$fields = $response.Outputs | Get-Member -MemberType NoteProperty | ForEach-Object Name
$outputArray = 0..($response.Outputs.$($fields[0]).Count - 1) | ForEach-Object {
    $idx = $_
    [PSCustomObject]$fields.ToDictionary({ $_ }, { $response.Outputs.$_[$idx] })
}

运行后得到的$outputArray就是你需要的按索引聚合的对象数组,直接导出、转JSON或者后续处理都可以直接使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 11:24:04