Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dai/e030a3ec7a0555d0eb3e3a167c9ba49e to your computer and use it in GitHub Desktop.
Save dai/e030a3ec7a0555d0eb3e3a167c9ba49e to your computer and use it in GitHub Desktop.
Get-Json Property - Powershell ⚡
# 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