Skip to content

Instantly share code, notes, and snippets.

@jlouros
Created August 13, 2019 10:05
Show Gist options
  • Save jlouros/c1e72d51676bc23e322d1f38e989b486 to your computer and use it in GitHub Desktop.
Save jlouros/c1e72d51676bc23e322d1f38e989b486 to your computer and use it in GitHub Desktop.
Outputs all JSON paths for a given JSON file
function Show-JsonPaths {
param(
[Parameter(Mandatory = $true)]
[System.Object]$custobj,
[string] $JPath = "$",
[switch] $OutputValues
)
foreach ($k in ($custobj | Get-Member -MemberType NoteProperty).Name) {
$isCustomType = $($custobj.$k -is [System.Management.Automation.PSCustomObject])
$isArray = $($custobj.$k -is [System.Object[]])
# keys with '.' need to be handled differently
if ($k -match "\." ) { $currentJPath = "$JPath['$k']" }
else { $currentJPath = "$JPath.$k" }
if ($isCustomType) {
Show-JsonPaths $custobj.$k -JPath $currentJPath -OutputValues:$OutputValues
}
elseif ($isArray -and ($custobj.$k[0] -is [System.Management.Automation.PSCustomObject])) {
for ($i = 0; $i -lt $custobj.$k.Length; $i++) {
Show-JsonPaths $custobj.$k[$i] -JPath "$currentJPath[$i]" -OutputValues:$OutputValues
}
}
else {
# output discovered data
if ($OutputValues -eq $true) { Write-Output "$currentJPath`t=>`t$($custobj.$k)" }
else { Write-Output "$currentJPath" }
}
}
}
Show-JsonPaths (ConvertFrom-Json (Get-Content -Raw $FULL_PATH_TO_JSON_FILE))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment