我通常使用JQ来处理JSON.

PS C:\> gc pw.txt
{
    "PowerShell": {
        "is a task": {
            "automation": [
                {
                    "and": "configuration",
                    "management": [
                        {
                            "program": "from",
                            "microsoft": {
                                "consisting of": "a command-line",
                                "shell": {
                                    "and": "the",
                                    "associated": {
                                        "scripting": {
                                            "language.": "initially a windows",
                                            "component": {
                                                "only": "known as",
                                                "windows": {
                                                    "powershell": "7.3.9-win-x64.msi",
                                                    "it was made": "open-source"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    ]
                },
                {
                    "the former": "is built on the .NET Framework",
                    "the latter on": ".NET (previously .NET Core)."
                }
            ]
        }
    }
} 

下面的方法在PowerShell中可以很好地从上面的JSON中获取值7.3.9-win-x64.msi:

PS C:\> gc pw.txt | jq -r '.[] | .[] | .[] | .[].management | .[0].microsoft | .shell.associated.scripting.component.windows.powershell'

那么,现在如何在不使用JQ的情况下获得相同的值,而是使用PowerShell本身中的内置实用程序(使用较短的命令尽可能多地使用)?

推荐答案

$jsonObject=gc pw.txt | ConvertFrom-JSON
$jsonObject.powershell."is a task".automation[0].management[0].microsoft.shell.associated.scripting.component.windows.powershell

Json相关问答推荐

使用jolt删除空对象

PostgreSQL 12.17从JSON数组提取元素

使用单元和非单元版本反序列化Rust中的枚举,而无需编写自定义反序列化程序

Python将Pandas转换为嵌套的JSON

Terraform迭代JSON文件以获取键值对

无法从JSON解析ZonedDateTime,但可以使用格式化程序很好地解析

在MongoDB中检索某个月份和年份的JSON文档

取消嵌套数组并将数组名称添加为附加字段

小写嵌套特定键的震动转换

我需要在 mongodb compass 中检索索引(编号 1)信息

转义 Haskell 记录的字段

从字节解码 JSON 数据,将 float 值更改为 int

从 PySpark 中的复杂 JSON 文件中高效清除 HTML 实体

如果 JSON 对象包含列表中的子字符串,则丢弃它们

SwiftUI:如何使用 0 索引数组键为 JSON 添加类型

Jolt - 在同一级别添加时组合值的问题

如何为所有 API 端点全局设置 http.ResponseWriter Content-Type 标头?

如何比较 JSON 文档并返回与 Jackson 或 Gson 的差异?

使用 Spring 和 JsonTypeInfo 注释将 JSON 反序列化为多态对象模型

IE中Json响应下载(7~10)