Last active
October 23, 2023 14:56
-
-
Save sassdawe/7caf25d05bd3dc094c272e86922a9734 to your computer and use it in GitHub Desktop.
Convert large well formatted JSON structures into [PSCustomObject[]]
This file contains hidden or 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-JSONContentX { | |
<# | |
.Synopsis | |
Get-JSONContentX | |
.DESCRIPTION | |
Get-JSONContentX, to convert eXtra large well formatted .json files into [PSCustomObject] | |
.EXAMPLE | |
Get-JSONContentX -Path C:\Source\temp.json -PropertyCount 4 | |
Will return the content to the console | |
.EXAMPLE | |
$entries = Get-JSONContentX -Path C:\Source\temp.json -PropertyCount 4 -Verbose | |
Will return the content into the $entries variable | |
#> | |
[OutputType([PSCustomObject[]])] | |
[CmdletBinding()] | |
param ( | |
[String] | |
$Path, | |
[int] | |
$PropertyCount | |
) | |
$rawContent = [System.IO.File]::ReadAllLines($path) | |
Write-Verbose "`$rawContent.count $($rawContent.count)" | |
# skip the first line, because it contains '[' | |
$i = 1 | |
# we need to drop the last two lines, with '},' and ']" | |
while ( $i -lt $rawContent.count - 1 ) { | |
# We have to put back that closing '}' we cut down with the comma | |
(($rawContent[$i..$($i + $PropertyCount)]) + '}' | Out-String) | ConvertFrom-Json | Foreach-Object { $_ } | |
$i += ($PropertyCount + 2) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment