我需要使用ps从杨森文件中提取一些信息.JSON嵌套了components个数组,这些数组也可以包含components个array.

{
"name": "app",
"components": [
    {
        "component_name": "comp1",
        "component_packages": [
            "comp1_package1",
            "comp1_package2"
        ],
        "project_id": "1234",
        "file_path": "requirements_file",
        "ref": "%%VERSION%%",
        "components": [
            {
                "component_name": "comp1.1",
                "component_packages": [
                    "comp1.1_package1"
                ],
                "project_id": "2345",
                "file_path": "requirements_file",
                "ref": "%%VERSION%%",
                "components": [
                    {
                        "component_name": "comp1.1.1",
                        "component_packages": [
                            "comp1.1.1_package1"
                        ],
                        "project_id": "3456",
                        "file_path": "requirements_file",
                        "ref": "%%VERSION%%",
                        "components": []
                    }]
            },
            {
                "component_name": "comp1.2",
                "component_packages": [
                    "comp1.2_package1"
                ],
                "project_id": "4567",
                "file_path": "requirements_file",
                "ref": "%%VERSION%%",
                "components": []
            }
        ]
    },
    {
        "component_name": "comp2",
        "component_packages": [
            "comp2_package1",
            "comp2_package2"
        ],
        "project_id": "5678",
        "file_path": "requirements_file",
        "ref": "%%VERSION%%",
        "components": [
            {
                "component_name": "comp2.1",
                "component_packages": [
                    "comp2.1_package1"
                ],
                "project_id": "6789",
                "file_path": "requirements_file",
                "ref": "%%VERSION%%",
                "components": []
            }
        ]
    }
]   
}

对于components中的每component个,我需要执行一个脚本来收集更多信息,但我很难一一处理iterating到所有元素.

我开始将SON转换为psobshot(Get-Content -Raw "$json_path" | ConvertFrom-Json)

我不想修复SON的深度.所以 playbook 应该具有适应性.

我try 使用while循环

$comp = $object.components
while ( $comp -ne "" ) { 
   $comp | ForEach-Object {
       # to something
   }
}

但这样不合适,因为即使我覆盖$comp,脚本也会forget一些条目.

推荐答案

只需使用一个函数并循环调用它

(注意:这可能不是最有效的方法)

$JsonObj = $Json | ConvertFrom-Json


function Drill-Json {
    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory,
            Position = 0,
            ValueFromPipeline,
            ValueFromPipelineByPropertyName,
            ValueFromRemainingArguments
        )]
        [array]$JsonComponent 
    )
        
    
    
    $JsonComponent | ForEach-Object {
        <# 

            DO STUFF

        #>
        # $_.component_name
        if (($_.components.count)) {
            Drill-Json -JsonComponent $_.components
        }
    
    }
}

Drill-Json -JsonComponent $JsonObj.components

Json相关问答推荐

需要Jolt规范将具有动态字段的输入json转换为输出json

Golang JSON Date Tim.Date()测试请求

无法访问id的第三级json

使用 jq 从带有转义反斜杠字符的嵌套 JSON 中提取数据

使用 jolt 变换压平具有公共列 JSON 的复杂嵌套

在 python 中循环 JSON 数组

迭代powershell双维json对象

将 JSON 字符串解析为 Kotlin Android 中的对象列表(MOSHI?)

无法在 json --data 中使用变量

Powershell 7.2:ConvertFrom-Json - 日期处理

如何在循环中传递列表(会话(字符串属性))以在 Gatling Scala 中创建批量 Json 请求

序列化为json时如何忽略空列表?

如何在 Node.js 中解析包含NaN的 JSON 字符串

Json.NET SerializeObject 转义值以防止 XSS

错误字符串的长度超过了maxJsonLength属性设置的值

如何在 Eclipse 中安装和使用 JSON 编辑器?

JSON日期到Java日期?

YAML 将 5e-6 加载为字符串而不是数字

使用 jQuery 和 JSON 填充表单?

Javascript对象和JSON对象有什么区别