-
-
Save dai/e030a3ec7a0555d0eb3e3a167c9ba49e to your computer and use it in GitHub Desktop.
Get-Json Property - Powershell ⚡
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# catjp - Concatenate JSON property | |
# Made with 💖 - Olwiba | |
function Get-JsonProperty { | |
param ( | |
[Parameter(Mandatory=$true)] | |
[string]$Path, | |
[Parameter(Mandatory=$true)] | |
[string]$Property | |
) | |
if (-Not (Test-Path $Path)) { | |
Write-Host "🛑 File not found at '$Path'." -ForegroundColor Red | |
return | |
} | |
try { | |
$jsonContent = Get-Content $Path | ConvertFrom-Json | |
if ($jsonContent.PSObject.Properties.Name -contains $Property) { | |
Write-Host "{" | |
$jsonContent.$Property.PSObject.Properties | ForEach-Object { | |
Write-Host -NoNewline (" $($_.Name): ") -ForegroundColor Green | |
Write-Host "`"$($_.Value)`"" | |
} | |
Write-Host "}" | |
} else { | |
Write-Host "🥲 Property '$Property' not found in JSON file at '$Path'." -ForegroundColor Yellow | |
} | |
} catch { | |
Write-Host "🚫 Failed to parse JSON from file at '$Path'. Ensure the file is a valid JSON document." -ForegroundColor Red | |
} | |
} | |
Set-Alias catjp Get-JsonProperty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment