0

I'm trying to dump a powershell array of hash tables to json like this:

$body= ,@($someArray | % {
        {
            @{
                name = $_
            }
        }
    })
$body | ConvertTo-Json 

But this yields this result:

{
    "value":  [
                  {
                      "name":  "test"
                  },
                  {
                      "name":  "test-2"
                  }
              ],
    "Count":  2
}

Not sure how to only get the value. If I do ConvertTo-Json | $body, I get:

[
    [
        {
            "name":  "test",
        },
        {
            "name":  "test-2",
        }
    ]
]

I'm doing the ,@() syntax because otherwise if I only have one object in my array it gets converted to an Hash Table instead of an array.

I am on Powershell 5.

mklement0
  • 382,024
  • 64
  • 607
  • 775
unitrium
  • 40
  • 1
  • 4
  • Does this answer your question? [Powershell Why the difference between the two json contents?](https://stackoverflow.com/questions/48841191/powershell-why-the-difference-between-the-two-json-contents) – mklement0 Jul 02 '22 at 19:52
  • Your question may have gotten little attention because you tagged it only as `powershell-5.0`, not also as `powershell`. – mklement0 Jul 02 '22 at 19:54

0 Answers0