Last active
July 15, 2024 09:25
-
-
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…
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
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.