0

I'm trying to write some code in PowerShell v5.1 that recurses through a parsed JSON object to get its property names. An example JSON object is below:

{
    "str1": "Hello World!",
    "strings": {
        "strA": "Foo!",
        "strB": "Bar!"
    },
    "others": {
        "myBool": true,
        "myInt": 42
    }
}

First I read and parse the object into a PSObject:

$jsonString = '{"str1": "Hello World!", "strings":{"strA": "Foo!","strB": "Bar!"}, "others":{"myBool": true,"myInt": 42}}'
$json = $jsonString | ConvertFrom-Json

Next I can use $json.PSobject.properties.name to get a list of all the top-level property names:

PS> $json.PSobject.properties.name
str1
strings
others

However, if I use $json.PSobject.properties.value, I only see the first two values output:

PS> $json.PSobject.properties.value
Hello World!

strA strB
---- ----
Foo! Bar!

I would expect to see the others property included here too. Is there a reason I'm not seeing it?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
eigenchris
  • 5,791
  • 2
  • 21
  • 30
  • 2
    This is the formatting subsystem interferring with your output - try outputting the in reverse: `@($json.PSobject.Properties)[2..0].value` :-) Pipe to `Format-List` to see all properties – Mathias R. Jessen Feb 01 '18 at 01:12
  • 1
    Alternatively output each value separately to `Out-Default`: `$json.PSobject.properties.value | % { $_ | Out-Default }`. – Ansgar Wiechers Feb 01 '18 at 10:10
  • This is because the properties of the objects in the pipeline are not aligned. See also: https://stackoverflow.com/questions/44428189/not-all-properties-displayed/44429084#44429084. Try: `$json.PSobject.properties.value |` [`union`](https://stackoverflow.com/a/44429084/1701026) – iRon Feb 01 '18 at 15:00

0 Answers0