0

Need help with how I can explicitly list the key names and its values for the below JSON using powershell.

    {
    "Data": {
        "OS_Support": {
            "SupportedOS": [
                "Microsoft Windows 10",
                "Microsoft Windows 11"
            ]
        },
        "MinFreeDiskSpace": {
            "SupportedDisk": 20
        },
        "MinMemorySize": {
            "SupportedMemory": 8
        },
        "App": {
            "Name": "Notepad",
            "Version": "1.2.3.4"
        }
    }
}

E.g. looking for output as below. Want to list the output with the last key name and its value.

SupportedOS      Microsoft Windows 10, Microsoft Windows 11
SupportedDisk    20
SupportedMemory  8
Name             Notepad
Version          1.2.3.4

I am trying to use below but it doesnt help. Any pointers would be really helpful,

(Get-Content -Path "JsonPath" | ConvertFrom-Json ).Data | Get-Member | Where-Object { $_.MemberType -match "Property" }
3
  • Do the Jsons always have the same structure? If yes, then there is no reason to take a dynamic approach Commented Jun 23, 2023 at 2:36
  • It wont be the same structure, hence want to know how to get this dynamic approach working.
    – Dhillli4u
    Commented Jun 23, 2023 at 3:25
  • See also: Recursive function to iterate through JSON Data
    – iRon
    Commented Jun 23, 2023 at 8:24

1 Answer 1

0

Extensive code but should be able to handle Jsons with different structures and output objects using the deepest key value pair as its properties.

$queue = [System.Collections.Generic.Queue[object]]::new()
$result = Get-Content test.json | ConvertFrom-Json | ForEach-Object {
    $queue.Enqueue($_)
    $tmp = [ordered]@{}

    while ($queue.Count) {
        foreach ($pso in $queue.Dequeue().PSObject.Properties) {
            $value = $pso.Value
            $isCollection = $false

            if ($value -is [System.Collections.ICollection]) {
                $isCollection = $true
                if ($value[0] -is [System.Management.Automation.PSCustomObject]) {
                    foreach ($item in $value) {
                        $queue.Enqueue($item)
                    }
                    continue
                }
            }

            if ($value -is [System.Management.Automation.PSCustomObject]) {
                $queue.Enqueue($value)
                continue
            }

            if ($isCollection) {
                $value = $value -join ', '
            }

            $tmp[$pso.Name] = $value
        }
    }

    [pscustomobject] $tmp
}

$result | Format-Table

Using the Json in your question the result would be:

SupportedOS                                SupportedDisk SupportedMemory Name    Version
-----------                                ------------- --------------- ----    -------
Microsoft Windows 10, Microsoft Windows 11            20               8 Notepad 1.2.3.4
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.