PowerShell 技能连载 - JSON 处理技巧

在 PowerShell 中处理 JSON 数据是一项常见任务,本文将介绍一些实用的 JSON 处理技巧。

首先,让我们看看基本的 JSON 操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 创建 JSON 信息获取函数
function Get-JSONInfo {
param(
[string]$JSONPath
)

try {
$json = Get-Content $JSONPath -Raw | ConvertFrom-Json

return [PSCustomObject]@{
FileName = Split-Path $JSONPath -Leaf
FileSize = (Get-Item $JSONPath).Length
PropertyCount = ($json.PSObject.Properties | Measure-Object).Count
IsArray = $json -is [Array]
Depth = Get-JSONDepth $json
}
}
catch {
Write-Host "获取 JSON 信息失败:$_"
}
}

# 获取 JSON 深度
function Get-JSONDepth {
param($Object)

if ($Object -is [Array]) {
return 1 + ($Object | ForEach-Object { Get-JSONDepth $_ } | Measure-Object -Maximum).Maximum
}
elseif ($Object -is [PSCustomObject]) {
return 1 + ($Object.PSObject.Properties | ForEach-Object { Get-JSONDepth $_.Value } | Measure-Object -Maximum).Maximum
}
return 0
}

JSON 数据验证:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 创建 JSON 数据验证函数
function Test-JSONData {
param(
[string]$JSONPath,
[string]$SchemaPath
)

try {
$json = Get-Content $JSONPath -Raw
$schema = Get-Content $SchemaPath -Raw

# 使用 Newtonsoft.Json 进行验证
Add-Type -Path "Newtonsoft.Json.dll"
$validator = [Newtonsoft.Json.Schema.JSchema]::Parse($schema)
$reader = [Newtonsoft.Json.JsonTextReader]::new([System.IO.StringReader]::new($json))

$valid = $validator.IsValid($reader)
$errors = $validator.Errors

return [PSCustomObject]@{
IsValid = $valid
Errors = $errors
}
}
catch {
Write-Host "验证失败:$_"
}
}

JSON 数据转换:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 创建 JSON 数据转换函数
function Convert-JSONData {
param(
[string]$InputPath,
[string]$OutputPath,
[ValidateSet("xml", "csv", "yaml")]
[string]$TargetFormat,
[hashtable]$Options
)

try {
$json = Get-Content $InputPath -Raw | ConvertFrom-Json

switch ($TargetFormat) {
"xml" {
$xml = [System.Xml.XmlDocument]::new()
$root = $xml.CreateElement("root")
$xml.AppendChild($root) | Out-Null

ConvertTo-XML $json $root $xml
$xml.Save($OutputPath)
}
"csv" {
if ($json -is [Array]) {
$json | ConvertTo-Csv -NoTypeInformation | Out-File $OutputPath
}
else {
[PSCustomObject]@{ $json.PSObject.Properties.Name = $json.PSObject.Properties.Value } |
ConvertTo-Csv -NoTypeInformation | Out-File $OutputPath
}
}
"yaml" {
# 使用 YamlDotNet 进行转换
Add-Type -Path "YamlDotNet.dll"
$yaml = [YamlDotNet.Serialization.Serializer]::new().Serialize($json)
$yaml | Out-File $OutputPath
}
}

Write-Host "数据转换完成:$OutputPath"
}
catch {
Write-Host "转换失败:$_"
}
}

# 将 JSON 对象转换为 XML
function ConvertTo-XML {
param($Object, $Parent, $Document)

if ($Object -is [Array]) {
foreach ($item in $Object) {
$element = $Document.CreateElement("item")
$Parent.AppendChild($element) | Out-Null
ConvertTo-XML $item $element $Document
}
}
elseif ($Object -is [PSCustomObject]) {
foreach ($property in $Object.PSObject.Properties) {
$element = $Document.CreateElement($property.Name)
$Parent.AppendChild($element) | Out-Null
ConvertTo-XML $property.Value $element $Document
}
}
else {
$Parent.InnerText = $Object.ToString()
}
}

JSON 数据查询:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 创建 JSON 数据查询函数
function Search-JSONData {
param(
[string]$JSONPath,
[string]$Query,
[ValidateSet("exact", "contains", "regex")]
[string]$MatchType = "contains"
)

try {
$json = Get-Content $JSONPath -Raw | ConvertFrom-Json
$results = @()

function Search-Object {
param($Object, $Path = "")

if ($Object -is [Array]) {
for ($i = 0; $i -lt $Object.Count; $i++) {
Search-Object $Object[$i] "$Path[$i]"
}
}
elseif ($Object -is [PSCustomObject]) {
foreach ($property in $Object.PSObject.Properties) {
$newPath = if ($Path) { "$Path.$($property.Name)" } else { $property.Name }
Search-Object $property.Value $newPath
}
}
else {
$value = $Object.ToString()
$matched = switch ($MatchType) {
"exact" { $value -eq $Query }
"contains" { $value -like "*$Query*" }
"regex" { $value -match $Query }
}

if ($matched) {
$results += [PSCustomObject]@{
Path = $Path
Value = $value
}
}
}
}

Search-Object $json
return $results
}
catch {
Write-Host "查询失败:$_"
}
}

这些技巧将帮助您更有效地处理 JSON 数据。记住,在处理 JSON 时,始终要注意数据的完整性和格式的正确性。同时,建议在处理大型 JSON 文件时使用流式处理方式,以提高性能。

PowerShell 技能连载 - JSON 处理技巧

http://blog.vichamp.com/2025/02/07/powershell-json-processing/

作者

吴波

发布于

2025-02-07

更新于

2025-03-25

许可协议

评论