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

PowerShell中如何动态按名称获取JSON子项值?

PowerShell动态提取JSON中指定配置项的字段值

问题描述

需要通过PowerShell遍历给定的JSON内容,实现**动态传入参数(如TP1或TP2)**来提取对应子项的字段值,当前使用的代码是否正确?

现有JSON文件结构

{   
 "TopicPropProfiles": [    
    {        
    "TP1":{            
        "enable-duplicate-detection": true,            
        "enable-batched-operations": true,            
        "enable-ordering": true      
        },        
    "TP2":{            
        "max-delivery-count": 3,            
        "enable-batched-operations": true      
        }    
    }    
],     
 "SubPropProfiles": [    
    {        
    "SP1":{            
        "enable-duplicate-detection": true,                        
        "max-size": 1024        
        },        
    "SP2":{            
        "max-delivery-count": 3,            
        "enable-batched-operations": true,                        
        "enable-session": false                  
        }    
    }    
],     
  "Topics":[    
        {        
            "TopicName": "topic1",        
            "SubNames": ["sub1","sub2","sub3"],        
            "TopicPropertyProfile": "TP1",        
            "SubPropertyProfile": "SP2" 
        },    
        {        
            "TopicName": "topic2",        
            "SubNames": ["sub4","sub5","sub6"],        
            "TopicPropertyProfile": "TP2",        
            "SubPropertyProfile": "SP1"    
        }
    ]
}   

当前使用的PowerShell代码

$profilejson =  Get-Content -Raw $profilePath | ConvertFrom-Json;
$profileObject = [PSCustomObject]$profilejson;

$TopicProps=$profileObject.TopicPropProfiles.TP1;
Write-Host $TopicProps.'enable-duplicate-detection'

回答

你当前的代码只能静态获取TP1的配置,无法支持动态传参,需要调整实现方式:

问题分析

  1. TopicPropProfiles是数组结构(外层包含[]),你直接用$profileObject.TopicPropProfiles.TP1虽然能拿到值,但本质是取数组第一个元素的TP1属性,写法不严谨。
  2. 静态指定TP1无法满足动态传参需求,必须用属性名动态引用的方式实现。

正确实现方式

方式1:变量动态指定属性名

先取出数组中的配置对象,再通过变量动态调用目标属性:

# 动态传入的目标配置项参数
$targetProfile = "TP2"

$profilejson = Get-Content -Raw $profilePath | ConvertFrom-Json
# 提取TopicPropProfiles数组的第一个元素(你的JSON中仅包含一个元素)
$topicPropConfig = $profilejson.TopicPropProfiles[0]
# 动态获取指定配置项
$TopicProps = $topicPropConfig.$targetProfile
# 输出目标字段值
Write-Host $TopicProps.'enable-duplicate-detection'

方式2:带存在性检查的动态获取

如果需要处理配置项不存在的场景,可以先枚举属性再判断:

$targetProfile = "SP1"
$profilejson = Get-Content -Raw $profilePath | ConvertFrom-Json
$subPropConfig = $profilejson.SubPropProfiles[0]

# 检查目标配置项是否存在
if ($subPropConfig.PSObject.Properties.Name -contains $targetProfile) {
    $SubProps = $subPropConfig.$targetProfile
    Write-Host $SubProps.'max-size'
} else {
    Write-Host "配置项 $targetProfile 不存在"
}

补充说明

  • ConvertFrom-Json本身就会返回PSCustomObject类型,不需要额外执行[PSCustomObject]$profilejson转换。
  • 如果JSON中TopicPropProfiles包含多个数组元素,需要循环遍历数组,再逐个处理每个元素内的配置项。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 17:20:27