首页 > 解决方案 > 如何使用 Powershell 将 json 输出转换为数组/列表(到 json 输出)

问题描述

想要使用 Powershell 将 json 输出转换为数组或列表。

我尝试使用 转换输出ConvertFrom-Json,然后获取键名称和值。需要建议。

json输出:

{
    "demo1": {
        "type": [
            "tuple",
            [
                [
                    "list",
                    "string"
                ],
                [
                    "list",
                    "string"
                ]
            ]
        ],
        "value": [
            [
                "123"
            ],
            [
                "456"
            ]
        ]
    },
    "demo2": {
        "type": [
            "tuple",
            [
                "string",
                "string"
            ]
        ],
        "value": [
            "abc",
            "xyz"
        ]
    }
}

使用 Powershell 想要将其转换如下:

demo1 = [["123"],["456"]]
demo2 = ["abc","xyz"]

提前致谢。

标签: powershell

解决方案


似乎是对 convertfrom-json 的一个非常简单的使用:

$a = get-content file.json | convertfrom-json

$a.demo1.value
123
456

$a.demo2.value
abc
xyz

你想要它作为json?

$a.demo1.value | ConvertTo-Json -Compress
[["123"],["456"]]

$a.demo2.value | ConvertTo-Json -Compress
["abc","xyz"]

推荐阅读