PerpetuallyConfused
PerpetuallyConfused

Reputation: 23

Convert JSON with a ton of values under each (property? object?) to CSV with many rows is returning all values in one string

I'm trying to use PowerShell to convert a json with the following format:

{"Value1":[89,91,88,71,59],"Value2":[53,58,54,53,58]}

To a CSV with the following format:

Value1,Value2
89,53
91,58
88,54
71,53
59,58

The issue I'm running into is that this code that I've cobbled together

Get-Content 'C:\test\test.json' | ConvertFrom-Json | 
select  @{n='value1';e={-split $_.value1}},
        @{n='value2';e={-split $_.value2}} |
Export-Csv -NoTypeInformation 'C:\test\test.csv'

is making a CSV with the following format:

Value1,Value2
89 91 88 71 59,53 58 54 53 58

Does anybody have any ideas on what I could do here? I'm really new to PowerShell and I'm getting pretty lost. I've read a ton of similar items on here and other sites, but none that exactly fit the issue I'm running into.

Thanks

Upvotes: 2

Views: 71

Answers (1)

Olaf
Olaf

Reputation: 5242

There might be more sophisticated solutions but at least for this simple example the following snippet works I think:

$Var = '{"Value1":[89,91,88,71,59],"Value2":[53,58,54,53,58]}' | ConvertFrom-Json 

$Var.Value1.Count
for ($i = 0; $i -lt $Var.Value1.Count; $i++) {
    [PSCustomObject]@{
        Value1 = $Var.Value1[$i]
        Value2 = $Var.Value2[$i]
    }
}

The output looks like this:

Value1 Value2
------ ------
    89     53
    91     58
    88     54
    71     53
    59     58

Upvotes: 2

Related Questions