Skip to content

Instantly share code, notes, and snippets.

@Olwiba
Last active March 5, 2024 02:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Olwiba/d2ccf99fd35249136fb4dc61aa6e81aa to your computer and use it in GitHub Desktop.
Save Olwiba/d2ccf99fd35249136fb4dc61aa6e81aa 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
@Olwiba
Copy link
Author

Olwiba commented Mar 4, 2024

example usage catjp ./package.json scripts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment