在PowerShell中序列化REST响应

Posted

技术标签:

【中文标题】在PowerShell中序列化REST响应【英文标题】:Serialize REST response in powershell 【发布时间】:2021-12-28 07:33:46 【问题描述】:

我正在连接到 REST 服务

$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"
    

在pythonic方法中,我可以这样做(包括“输出”键):

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

pprint(outputs)

但在 powershell 中我不知道该怎么做。你能把我引向正确的方向吗?

使用完整的 $response.outputs 来自 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

然后我可以转换成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"
                      ]

这就是说waitingforguacamole 解决方案虽然有点棘手(当然,感谢您的帮助!)

【问题讨论】:

请向我们展示代码返回的(已清理的)和 有效 json,保持结构完整 我改进了映射器以处理有序字段列表,并提供了以下代码的精简版本。 映射器的好主意! 【参考方案1】:

好的,我试了一下,也许不太优雅(它既不是函数式也不是 Pythonic),我相信其他人会有更具表现力的方法这个,但这可能是一个开始:

我将 JSON 清理为如下所示


    "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,
        7
    ]

(为了完整起见,我给NumScrapsList添加了一个值,并将每个***JSON字段转换为一个数组)

那么,

#simulate your REST method call result
$json = "
    `"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,
        7
    ]
"

#create a field map
$fieldMap = @("NumScrapsList", "Start", "Actual")

#convert the JSON to a Powershell object, create an empty array
$in = $json | ConvertFrom-JSON

#using NumScrapsList as your iterator
0..($in.NumScrapsList.Count-1) | ForEach-Object 
    $fieldMap | ForEach-Object -Begin 
        #reference the outer loop index
        $index = $_
        #initialize an accumulator for the object whose properties to be mapped
        $obj = @
     -Process 
        #in order of fieldMap's properties, grab those fields from the input
        #and add them to the accumulator by name
        $obj."$_" = $in."$_"[$index]
     -End 
        #return that accumulator
        $obj
    
 | ConvertTo-Json

并且该处理块可以简化为:

$fieldMap = @("NumScrapsList", "Start", "Actual"); 
$in = $json | ConvertFrom-JSON
0..($in.NumScrapsList.Count-1) | ForEach-Object  
    $fieldMap | ForEach-Object  $index = $_; $obj = @  
        $obj."$_" = $in."$_"[$index]
      $obj 
 | ConvertTo-Json

【讨论】:

以上是关于在PowerShell中序列化REST响应的主要内容,如果未能解决你的问题,请参考以下文章

序列化多个模型并在一个 json 响应中发送全部 django rest 框架

将 Spring REST 端点配置为在序列化响应对象中的日期字段时忽略附加时区

从 django rest 框架中的序列化程序发送自定义错误响应?

WCF -Rest- DataContract:反序列化 XML 包装的响应

Django Rest Framework:序列化程序响应错误

反序列化 Facebook Workplace SCIM Rest API 响应