How to read JSON data from Powershell script and iterate through it

Viewed 13910

I'm starting to learn Powershell and I'm currently trying to read in a JSON file.

Here is my JSON file (named 'versions.json'):

{
    "versions": {
        "1.0.0": {
            "Component1": "1.0.0",
            "Component2": "1.0.0",
            "Component3": "1.0.0",
        },
        "2.0.0": {
            "Component1": "2.0.0",
            "Component2": "2.0.0",
            "Component3": "2.0.0"
        }
    }
}

I would like to read in this JSON file and print out the versions and what they consist of. For example, 1.0.0 consists of Component 1 at 1.0.0, Component 2 at 1.0.0, and Component 3 at 1.0.0.

I'm currently reading in the JSON file with this Powershell line:

$json = (Get-Content "versions.json" -Raw) | ConvertFrom-Json

Now, I want to iterate through $json and print out its data. I'm currently using this:

foreach($v in $json.versions) {
    echo "Data: $v"
}

But, when I run my Powershell script, it prints:

Data: @{1.0.0=; 2.0.0=}

Is this the proper output? I was expecting to at least see two entries for 1.0.0 and 2.0.0. This feels like it may be a syntax issue but I am unsure. I am using Powershell version 5.

1 Answers
Related