Skip to content

Commit b2c2657

Browse files
committed
🐛 Select-Json.ps1 fails to select within arrays property
Fixes #35
1 parent 9ff0fa6 commit b2c2657

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

Select-Json.ps1

+21-7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ AD~BC
5959
'{d:{a:{b:1,c:{"$ref":"#/d/c"}},c:{d:{"$ref":"#/d/two"}},two:2}}' |Select-Json.ps1 /*/a/c/* -FollowReferences
6060
6161
2
62+
63+
.EXAMPLE
64+
Resolve-Path $env:LOCALAPPDATA/Packages/*WindowsTerminal*/LocalState/settings.json |Get-Item |Get-Content -Raw |Select-Json.ps1 /profiles/list/*/name
65+
66+
PowerShell
67+
Windows PowerShell
68+
Ubuntu
69+
F# Interactive
70+
F# REPL
71+
C# REPL
72+
Command Prompt
73+
Media Server (ssh)
74+
Azure Cloud Shell
75+
Developer Command Prompt for VS 2022
76+
Developer PowerShell for VS 2022
6277
#>
6378

6479
#Requires -Version 7
@@ -153,10 +168,10 @@ Begin
153168
if($refUri) {return Get-Reference -ReferenceUri $refUri -Root $Script:Root |Select-Pointer -Segments $Segments}
154169
}
155170
if(!$Segments.Count) {return $InputObject}
156-
$segment,$Segments = $Segments
157-
if($null -eq $Segments) {[string[]]$Segments = @()}
158-
if($segment -match '[?*[]') {Select-Wildcard $InputObject $segment |Select-Pointer -Segments $Segments}
159-
else {Select-Segment $InputObject $segment |Select-Pointer -Segments $Segments}
171+
$segment,$tail = $Segments
172+
if($null -eq $tail) {[string[]]$tail = @()}
173+
if($segment -match '[?*[]') {Select-Wildcard $InputObject $segment |Select-Pointer -Segments $tail}
174+
else {Select-Segment $InputObject $segment |Select-Pointer -Segments $tail}
160175
}
161176
}
162177
Process
@@ -172,9 +187,8 @@ Process
172187
if($InputObject -is [string])
173188
{
174189
if($InputObject.StartsWith(([char]0xFEFF))) {$InputObject = $InputObject.Substring(1)}
175-
return $InputObject |
176-
ConvertFrom-Json -AsHashtable |
177-
Select-Json.ps1 -JsonPointer $JsonPointer -FollowReferences:$FollowReferences
190+
return Select-Json.ps1 -JsonPointer $JsonPointer -FollowReferences:$FollowReferences `
191+
-InputObject ($InputObject |ConvertFrom-Json -AsHashtable -NoEnumerate)
178192
}
179193
if(!$jsonpath.Length) {return $InputObject}
180194
$Script:Root = $InputObject

test/Select-Json.Tests.ps1

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ Describe 'Select-Json' -Tag Select-Json -Skip:$skip {
2222
@{ Json = '{"a":1, "b": {"ZZ/ZZ": {"AD~BC": 7}}}'; Pointer = '/b/ZZ~1ZZ/AD~0BC'; Result = 7 }
2323
@{ Json = '{"a":1, "b": {"[*?/]": {"AD~BC": 7}}}'; Pointer = '/b/~4~3~2~1]/AD~0BC'; Result = 7 }
2424
@{ Json = '{a:1}'; Pointer = '/b'; Result = $null }
25+
@{ Json = '{name: true, id: false, description: false}'; Pointer = '/name'; Result = $true }
26+
@{ Json = '[{name: true, id: false, description: false}]'; Pointer = '/0/name'; Result = $true }
27+
@{ Json = '[{name: true, id: false, description: false}]'; Pointer = '/*/name'; Result = $true }
28+
@{ Json = '{list:[{name: true, id: false}]}'; Pointer = '/list/0/name'; Result = $true }
29+
@{ Json = '{list:[{name: true, id: false}]}'; Pointer = '/list/*/name'; Result = $true }
2530
) {
2631
Param([string] $Json, [string] $Pointer, $Result)
2732
$Json |Select-Json.ps1 -JsonPointer $Pointer |Should -BeExactly $Result -Because 'pipeline should work'

0 commit comments

Comments
 (0)