Skip to content

Instantly share code, notes, and snippets.

@dfinke
Last active July 15, 2024 09:25
Show Gist options
  • Save dfinke/5271858 to your computer and use it in GitHub Desktop.
Save dfinke/5271858 to your computer and use it in GitHub Desktop.
This function works like PowerShell's builtin cmdlet Get-Content, except, if you pass in a file that contains JSON, it will convert it for you. If for example, you are working with npm modules (node.js packages), they have a file package.json that are used for configuration information. So you can use it like this: ls . -recurse *.json | Get-Con…
function Get-ContentJson {
<#
.Synopsis
Get-ContentJson reads a json file, converts it an returns the object
.Example
ls . -recurse *.json | Get-ContentJson
.Example
Get-ContentJson (dir . *.json).fullname
#>
param(
[Parameter(ValueFromPipeLineByPropertyName)]
$FullName
)
Process {
[System.IO.File]::ReadAllText($FullName) |
ConvertFrom-Json
}
}
@smurawski
Copy link

If you follow the naming convention for similar commands, wouldn't Import-Json be more appropriate? Following the lines of Import-CSV and Import-CliXml? Both of those commands read a file and deserialize objects from their specified format.

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