jarrad_obrien
jarrad_obrien

Reputation: 446

How to display full PSCustomObject that has been converted from JSON

I have a JSON file that I need to manipulate using PowerShell. I convert it to a PSCustomObject with:

$template = Get-Content $filePath -raw | ConvertFrom-Json

When I try to print out $template with:

Write-Host $template

I get:

@{$parameters=; variables=; resources=System.Object[]}

There are more key-values inside of parameters, variables, and resources that I want to see. How can I print out the entire expanded file?

Also, how can I print out the value of a particular key? For example, if I just wanted to print out parameters, which itself could contain more key-value pairs.

Upvotes: 0

Views: 558

Answers (1)

iRon
iRon

Reputation: 23663

Example

In my answer I am using this Json example (shown below) as you do not have an example in your question:

$template = ConvertFrom-Json @'
{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}
'@

Formatting complex objects

As you can see here, the original Json file is actually already a good representation of how the object hierarchy looks like. This is obviously in Json (JavaScript) format. If you want to display it as a reversible PowerShell Expression, you might want to use the PowerShell function ConvertTo-Expression from the PowerShell Gallery.
Usage example:

[PSCustomObject]@{glossary = [PSCustomObject]@{
        title = 'example glossary'
        GlossDiv = [PSCustomObject]@{
            title = 'S'
            GlossList = [PSCustomObject]@{GlossEntry = [PSCustomObject]@{
                    ID = 'SGML'
                    SortAs = 'SGML'
                    GlossTerm = 'Standard Generalized Markup Language'
                    Acronym = 'SGML'
                    Abbrev = 'ISO 8879:1986'
                    GlossDef = [PSCustomObject]@{
                        para = 'A meta-markup language, used to create markup languages such as DocBook.'
                        GlossSeeAlso =
                            'GML',
                            'XML'
                    }
                    GlossSee = 'markup'
                }}
        }
    }}

Accessing Json object properties and array items

Which regards to printing a particular key:

To refer to an object property:

$template.Glossary.Title
example glossary

To refer to an array item (in a chain of objects):

$template.Glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso[1]
XML

Upvotes: 1

Related Questions