-
Notifications
You must be signed in to change notification settings - Fork 29
/
Select-Json.ps1
196 lines (169 loc) · 5.61 KB
/
Select-Json.ps1
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<#
.SYNOPSIS
Returns a value from a JSON string or file.
.INPUTS
System.String containing JSON, or
System.Collections.Hashtable parsed from JSON, or
System.Management.Automation.PSObject parsed from JSON.
.OUTPUTS
System.Boolean, System.Int64, System.Double, System.String,
System.Management.Automation.PSObject, or
System.Management.Automation.OrderedHashtable (or null) selected from JSON.
.FUNCTIONALITY
Json
.LINK
https://www.rfc-editor.org/rfc/rfc6901
.LINK
ConvertFrom-Json
.EXAMPLE
'true' |Select-Json.ps1 # default selection is entire parsed JSON document
True
.EXAMPLE
'{"":3.14}' |Select-Json.ps1 /
3.14
.EXAMPLE
Select-Json.ps1 /powershell.codeFormatting.preset -Path ./.vscode/settings.json
Allman
.EXAMPLE
'{"a":1, "b": {"ZZ/ZZ": {"AD~BC": 7}}}' |Select-Json.ps1 /b/ZZ~1ZZ
Name Value
---- -----
AD~BC 7
.EXAMPLE
'{"a":1, "b": {"ZZ/ZZ": {"AD~BC": 7}}}' |ConvertFrom-Json |Select-Json.ps1 /b/ZZ~1ZZ
AD~BC
-----
7
.EXAMPLE
'{"a":1, "b": {"ZZ/ZZ": {"AD~BC": 7}}}' |Select-Json.ps1 /b/ZZ~1ZZ |ConvertTo-Json -Compress
{"AD~BC":7}
.EXAMPLE
'{d:{a:{b:1,c:{"$ref":"#/d/c"}},c:{d:{"$ref":"#/d/two"}},two:2}}' |Select-Json.ps1 /*/a/c/* -FollowReferences
2
.EXAMPLE
Resolve-Path $env:LOCALAPPDATA/Packages/*WindowsTerminal*/LocalState/settings.json |Get-Item |Get-Content -Raw |Select-Json.ps1 /profiles/list/*/name
PowerShell
Windows PowerShell
Ubuntu
F# Interactive
F# REPL
C# REPL
Command Prompt
Media Server (ssh)
Azure Cloud Shell
Developer Command Prompt for VS 2022
Developer PowerShell for VS 2022
#>
#Requires -Version 7
[CmdletBinding()][OutputType([bool],[long],[double],[string],[Management.Automation.OrderedHashtable])] Param(
<#
The full path name of the property to get, as a JSON Pointer,
modified to support wildcards: ~0 = ~ ~1 = / ~2 = ? ~3 = * ~4 = [
#>
[Parameter(Position=0)][Alias('Name')][AllowEmptyString()][ValidatePattern('\A(?:|/(?:[^~]|~[0-4])*)\z')]
[string] $JsonPointer = '',
# The JSON (string or parsed object/hashtable) to get the value from.
[Parameter(ParameterSetName='InputObject',ValueFromPipeline=$true)] $InputObject,
# A JSON file to update.
[Parameter(ParameterSetName='Path',Mandatory=$true)][string] $Path,
# Indicates that references should be followed.
[Alias('FollowRefs','References')][switch] $FollowReferences
)
Begin
{
[string[]] $jsonpath = switch($JsonPointer) { '' {,@()}
default {,@($_ -replace '\A/' -replace '~4','[[]' -replace '~3','[*]' -replace '~2','[?]' -split '/' -replace '~1','/' -replace '~0','~')} }
function Get-ReferenceUri
{
[CmdletBinding()] Param($InputObject)
if($InputObject -is [array]) {return}
if($InputObject -is [Collections.IDictionary] -and $InputObject.ContainsKey('$ref')) {return $InputObject['$ref']}
if($InputObject.PSObject.Properties.Match('$ref').Count) {return $InputObject.'$ref'}
}
function Get-Reference
{
[CmdletBinding()] Param([uri] $ReferenceUri, $Root)
$source,$pointer = switch($ReferenceUri)
{
{$null -eq $_} {$Root,''; continue}
{$_.OriginalString -like '#*'} {$Root,($_.OriginalString -replace '\A#')}
{$_.IsFile} {(Get-Content $_.LocalPath -Raw |ConvertFrom-Json -AsHashtable),($_.Fragment -replace '\A*')}
default {(Invoke-RestMethod $ReferenceUri),($_.Fragment -replace '\A#')}
}
return $source |Select-Json.ps1 $pointer
}
function Select-Segment
{
[CmdletBinding()] Param([Parameter(Position=0)] $InputObject, [Parameter(Position=1)][string] $Segment)
if($InputObject -is [array])
{
if(![int]::TryParse($Segment,[ref]$Segment)) {return}
elseif($InputObject.Length -le $Segment) {return}
else {return,$InputObject[$Segment]}
}
elseif($InputObject -is [Collections.IDictionary])
{
if(!$InputObject.ContainsKey($Segment)) {return}
else {return,$InputObject[$Segment]}
}
else
{
return,($InputObject.PSObject.Properties.Match($Segment) |
Select-Object -ExpandProperty Value)
}
}
function Select-Wildcard
{
[CmdletBinding()] Param([Parameter(Position=0)] $InputObject, [Parameter(Position=1)][string] $Segment)
if($InputObject -is [array])
{
return 0..($InputObject.Length-1) -like $Segment |ForEach-Object {$InputObject[$_]}
}
elseif($InputObject -is [Collections.IDictionary])
{
return $InputObject.Keys -like $Segment |ForEach-Object {$InputObject[$_]}
}
else
{
return $InputObject.PSObject.Properties.Match($Segment) |
Select-Object -ExpandProperty Value
}
}
filter Select-Pointer
{
[CmdletBinding()] Param(
[Parameter(ValueFromPipeline=$true)] $InputObject,
[string[]] $Segments
)
if($FollowReferences)
{
$refUri = Get-ReferenceUri $InputObject
if($refUri) {return Get-Reference -ReferenceUri $refUri -Root $Script:Root |Select-Pointer -Segments $Segments}
}
if(!$Segments.Count) {return $InputObject}
$segment,$tail = $Segments
if($null -eq $tail) {[string[]]$tail = @()}
if($segment -match '[?*[]') {Select-Wildcard $InputObject $segment |Select-Pointer -Segments $tail}
else {Select-Segment $InputObject $segment |Select-Pointer -Segments $tail}
}
}
Process
{
if($Path)
{
return Resolve-Path -Path $Path |
Get-Content -Raw |
ForEach-Object {$_ |ConvertFrom-Json -AsHashtable |
Select-Json.ps1 -JsonPointer $JsonPointer -FollowReferences:$FollowReferences}
}
if($null -eq $InputObject) {return}
if($InputObject -is [string])
{
if($InputObject.StartsWith(([char]0xFEFF))) {$InputObject = $InputObject.Substring(1)}
return Select-Json.ps1 -JsonPointer $JsonPointer -FollowReferences:$FollowReferences `
-InputObject ($InputObject |ConvertFrom-Json -AsHashtable -NoEnumerate)
}
if(!$jsonpath.Length) {return $InputObject}
$Script:Root = $InputObject
return Select-Pointer -InputObject $InputObject -Segments $jsonpath
}