Skip to content

Commit

Permalink
✨ 🐛 Add array and wildcard support to JSON brianary#27 brianary#28
Browse files Browse the repository at this point in the history
  • Loading branch information
brianary committed Jan 30, 2024
1 parent 8ca5006 commit dacb465
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
11 changes: 7 additions & 4 deletions Export-Json.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ https://www.rfc-editor.org/rfc/rfc6901
#Requires -Version 7
[CmdletBinding()] Param(
<#
The full path name of the property to get, as a JSON Pointer, which separates each nested
element name with a /, and literal / is escaped as ~1, and literal ~ is escaped as ~0.
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|~1)*)\z')]
[Parameter(Position=0)][Alias('Name')][AllowEmptyString()][ValidatePattern('\A(?:|/(?:[^~]|~[0-4])*)\z')]
[string] $PropertyName = '',
# The JSON (string or parsed object/hashtable) to get the value from.
[Parameter(ParameterSetName='InputObject',ValueFromPipeline=$true)] $InputObject
[Parameter(ParameterSetName='InputObject',ValueFromPipeline=$true)] $InputObject,
# A JSON file to update.
[Parameter(ParameterSetName='Path',Mandatory=$true)][string] $Path
)

function Get-Reference
Expand Down Expand Up @@ -101,6 +103,7 @@ filter Import-Reference
return $InputObject
}

if($Path) {$InputObject = Get-Content $Path -Raw}
$root = $InputObject -is [string] ? ($InputObject |ConvertFrom-Json) : $InputObject
$selection = $root |Select-Json.ps1 $PropertyName
return $selection |Import-Reference -Root $root |ConvertTo-Json -Depth 100
65 changes: 53 additions & 12 deletions Select-Json.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ AD~BC
#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, which separates each nested
element name with a /, and literal / is escaped as ~1, and literal ~ is escaped as ~0.
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|~1)*)\z')]
[Parameter(Position=0)][Alias('Name')][AllowEmptyString()][ValidatePattern('\A(?:|/(?:[^~]|~[0-4])*)\z')]
[string] $PropertyName = '',
# The JSON (string or parsed object/hashtable) to get the value from.
[Parameter(ParameterSetName='InputObject',ValueFromPipeline=$true)] $InputObject,
Expand All @@ -87,20 +87,61 @@ element name with a /, and literal / is escaped as ~1, and literal ~ is escaped
Begin
{
[string[]] $jsonpath = switch($PropertyName) { '' {,@()} '/' {,@('')}
default {,@($_ -replace '\A/' -split '/' -replace '~1','/' -replace '~0','~')} }
default {,@($_ -replace '\A/' -replace '~4','[[]' -replace '~3','[*]' -replace '~2','[?]' -split '/' -replace '~1','/' -replace '~0','~')} }

filter Select-Next
{
[CmdletBinding()] Param(
[Parameter(ValueFromPipeline=$true)] $InputObject,
[string[]] $Segments
)
if(!$Segments.Count) {return $InputObject}
$segment,$Segments = $Segments
if($null -eq $Segments) {[string[]]$Segments = @()}
if($segment -match '[?*[]')
{
if($InputObject -is [array])
{
return 0..($InputObject.Length-1) -like $segment |ForEach-Object {$InputObject[$_]} |Select-Next -Segments $Segments
}
elseif($InputObject -is [Collections.IDictionary])
{
return $InputObject.Keys -like $segment |ForEach-Object {$InputObject[$_]} |Select-Next -Segments $Segments
}
else
{
return $InputObject.PSObject.Properties.Match($segment) |
Select-Object -ExpandProperty Value |
Select-Next -Segments $Segments
}
}
else
{
if($InputObject -is [array])
{
if(![int]::TryParse($segment,[ref]$segment)) {return}
elseif($InputObject.Length -le $segment) {return}
else {return Select-Next -InputObject ($InputObject[$segment]) -Segments $Segments}
}
elseif($InputObject -is [Collections.IDictionary])
{
if(!$InputObject.ContainsKey($segment)) {return}
else {return Select-Next -InputObject ($InputObject[$segment]) -Segments $Segments}
}
else
{
return $InputObject.PSObject.Properties.Match($segment) |
Select-Object -ExpandProperty Value |
Select-Next -Segments $Segments
}
}
}
}
Process
{
if($Path) {return Get-Content -Path $Path -Raw |ConvertFrom-Json -AsHashtable |Select-Json.ps1 -PropertyName $PropertyName}
if($null -eq $InputObject) {return}
if($InputObject -is [string]) {return $InputObject |ConvertFrom-Json -AsHashtable |Select-Json.ps1 -PropertyName $PropertyName}
if(!$jsonpath.Length) {return $InputObject}
$selection,$hashmode = $InputObject,($InputObject -is [Collections.IDictionary])
foreach($segment in $jsonpath)
{
if($hashmode) {if(!$selection.ContainsKey($segment)) {return}}
elseif(!$selection.PSObject.Properties.Match($segment).Count) {return}
$selection = $selection.$segment
}
return $selection
return Select-Next -InputObject $InputObject -Segments $jsonpath
}

0 comments on commit dacb465

Please sign in to comment.