Arleigh Reyna
Arleigh Reyna

Reputation: 369

Parse JSON Nested SubValues in Powershell to Table

I converted the JSON string to Powershell in v5. The original json string is below:

$j = @'
[{
    "id": "1",
    "Members": [
        "A",
        "B",
        "C"
    ]
}, {
    "id": "2",
    "Members": [
        "A",
        "C"
    ]
}, {
    "id": "3",
    "Members": [
        "A",
        "D"
    ]
}]
'@

$json = $j | ConvertFrom-Json

I would like the result set to look like the result set below. Eventually I will export to SQL:

 id     Members
-----   --------
1       A
1       B
1       C
2       A
2       C
3       A
3       D

Upvotes: 0

Views: 495

Answers (1)

Anthony Stringer
Anthony Stringer

Reputation: 2001

try this

$json | % {
    $id = $_.id
    $_.members | select @{n='id';e={$id}}, @{n='members';e={$_}}
}

Upvotes: 1

Related Questions