使用PowerShell将JSON数据转换为属性格式
我明白你现在的困境——需要把JSON转换成特定的属性格式并集成到Azure Pipeline里,在线工具没法满足自动化的需求对吧?别担心,这里有几个实用的解决方案,你可以直接用到Pipeline中:
方案1:使用Python脚本(推荐,处理复杂JSON更灵活)
Python对JSON的处理非常友好,而且Azure Pipeline原生支持Python任务。下面是一个可以实现你需求的脚本:
import json def json_to_properties(data, parent_key=''): properties = [] for key, value in data.items(): current_key = f"{parent_key}.{key}" if parent_key else key if isinstance(value, dict): properties.extend(json_to_properties(value, current_key)) elif isinstance(value, list): for index, item in enumerate(value): list_key = f"{current_key}.{index}" properties.extend(json_to_properties(item, list_key)) else: # 处理字符串类型,添加引号;数字和布尔值直接输出 if isinstance(value, str): properties.append(f'{current_key}="{value}"') else: properties.append(f'{current_key}={value}') return properties # 示例输入(你可以替换成从文件或Pipeline变量读取的JSON) input_json = ''' { "name": "John", "age": 30, "married": true, "cars": [ {"model": "BMW 230", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 24.1} ] } ''' # 解析JSON并转换 data = json.loads(input_json) properties_list = json_to_properties(data) result = ' '.join(properties_list) # 输出结果(可以写入文件或设置为Pipeline变量) print(result)
在Azure Pipeline中使用:
- 在你的代码仓库中创建一个
json-to-props.py文件,把上面的代码保存进去。 - 在Pipeline的YAML文件中添加一个Python任务:
- task: PythonScript@0 inputs: scriptSource: 'filePath' scriptPath: '$(System.DefaultWorkingDirectory)/json-to-props.py' arguments: '' # 如果需要传递JSON路径或变量,可以在这里添加
- 如果需要将结果保存为Pipeline变量供后续步骤使用,可以在脚本最后添加:
print(f"##vso[task.setvariable variable=jsonProperties]{result}")
之后就能用$(jsonProperties)引用这个变量了。
方案2:使用PowerShell脚本(适合Windows环境)
如果你更熟悉PowerShell,也可以用下面的脚本实现:
function Convert-JsonToProperties { param( [Parameter(Mandatory=$true)] [string]$JsonInput, [string]$ParentKey = '' ) $properties = @() $data = $JsonInput | ConvertFrom-Json -Depth 100 foreach ($key in $data.PSObject.Properties.Name) { $value = $data.$key $currentKey = if ($ParentKey) { "$ParentKey.$key" } else { $key } if ($value -is [PSCustomObject]) { $properties += Convert-JsonToProperties -JsonInput ($value | ConvertTo-Json -Depth 100) -ParentKey $currentKey } elseif ($value -is [array]) { for ($i=0; $i -lt $value.Count; $i++) { $listKey = "$currentKey.$i" $properties += Convert-JsonToProperties -JsonInput ($value[$i] | ConvertTo-Json -Depth 100) -ParentKey $listKey } } else { if ($value -is [string]) { $properties += "$currentKey=`"$value`"" } else { $properties += "$currentKey=$value" } } } return $properties } # 示例输入 $inputJson = @" { "name": "John", "age": 30, "married": true, "cars": [ {"model": "BMW 230", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 24.1} ] } "@ # 转换并输出 $result = (Convert-JsonToProperties -JsonInput $inputJson) -join ' ' Write-Host $result # 可选:设置为Pipeline变量 Write-Host "##vso[task.setvariable variable=jsonProperties]$result"
在Azure Pipeline中使用:
添加一个PowerShell任务到YAML中:
- task: PowerShell@2 inputs: targetType: 'filePath' filePath: '$(System.DefaultWorkingDirectory)/json-to-props.ps1'
这两个方案都能完美实现你需要的转换效果,而且可以无缝集成到Azure Pipeline的自动化流程中。你可以根据自己的技术栈选择合适的方案~
内容的提问来源于stack exchange,提问作者Ruslan Imadiyev
相关产品推荐
相关产品推荐

