Alexander Sinno
Alexander Sinno

Reputation: 564

PowerShell - Enumerating Key Value Pairs when deserialized by .NET object

Currently, I am working to enumerator values from a Key Value pair correctly from the structure below. However, since it's such a large JSON file I am not using Invoke-RestMethod since I believe it's limited. To bypass this I used the following .NET object

System.Web.Script.Serialization.JavaScriptSerializer

 [void][System.Reflection.Assembly]::LoadWithPartialName('System.Web.Extensions')        
 $jsonserial= New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer 
 $jsonserial.MaxJsonLength  = 67108864

The problem with this is that I am being presented with Key-Value Pairs that I don't have a lot of experience with. Thus, I am getting an unwanted structure.

The results I am getting are as below:

Key        Value               
---        -----               
age         28    
sex         Male      
age         27
sex         Female         
age         30
sex         Male     
age         32 
sex         Female

Here is what I am trying to accomplish:

Age      Sex
---      ---
28       Male
27       Female
30       Male
32       Female

I tried enumerating the object with the foreach statement

$jsonObject | 
  ForEach-Object {
   foreach ($p in $_.PSObject.Properties) 
     {
      $p.Value
        } 
 } | Format-Table

But my response looks like this:

System.Collections.Generic.GenericEqualityComparer`1[System.String]
2
age
sex
28
Male
False
False
System.Object
False
System.Collections.Generic.GenericEqualityComparer`1[System.String]

Upvotes: 1

Views: 2182

Answers (2)

Alexander Sinno
Alexander Sinno

Reputation: 564

Thanks for all of those who helped out on this issue. After some research, I found that the answer is to join the JSON file with -join "". For example after adding the .NET Object from above you need to then deserialize the object and use -join ""

Example:

$personnelReport = $jsonserial.DeserializeObject($jsonFile -join '')

Upvotes: 0

woxxom
woxxom

Reputation: 73616

ConvertFrom-Json can handle large files but it's ~3 times slower than JavaScriptSerializer which returns hashtables, not PS custom objects (9 seconds vs 3 on a 57MB .json file here).

Assuming the JSON is an array of non-recursive records like these:

[
    {
        "age":  28,
        "sex":  "Male"
    },
    {
        "age":  27,
        "sex":  "Female"
    },
    {
        "age":  30,
        "sex":  "Male"
    },
    {
        "age":  32,
        "sex":  "Female"
    }
]

Convert it using [PSCustomObject] type accelerator available since PowerShell 3.0:

$users = foreach ($user in $jsonObject) { [PSCustomObject]$user }

or in PowerShell 2.0:

$users = foreach ($user in $jsonObject) { New-Object PSObject -Property $user }

With the conversion it's about 1.5 times faster than ConvertFrom-Json.
Depending on the actual data, it might be possible to process hashtables directly without conversion.

Upvotes: 3

Related Questions