Skip to content

ConvertFrom-Json and ConvertTo-Json eat the one object array #2448

@SummerSun

Description

@SummerSun

OS info

Edition: Windows 10 Enterprise
Version: 1607
OS Build: 14393.187
Installed RAM: 16.0 GB
System Type: 64-bit operating system, x64-based processor

Steps to reproduce

  1. Create a .json file. Write in an array with only one valid json object:
[{
    "name": "Animals",
    "age": "1",
    "children": [{
        "name": "mimi",
        "age": "1",
        "lovely": true,
        "height": 20
    },
    {
        "name": "dog",
        "age": "2",
        "lovely": false,
        "height": 100
    }]
}]
  1. Convert this json file content to psobject and then convert it back to write it to another file
$o=(Get-Content -Raw D:\Test_Convert.json | ConvertFrom-Json)
Set-Content 'D:\result.json' ($o | ConvertTo-Json)
  1. View the result.json:
{
    "name":  "Animals",
    "age":  "1",
    "children":  [
                     {
                         "name":  "mimi",
                         "age":  "1",
                         "lovely":  true,
                         "height":  20
                     },
                     {
                         "name":  "dog",
                         "age":  "2",
                         "lovely":  false,
                         "height":  100
                     }
                 ]
}

Expected behavior

Get the right PSObject and write the same content back.

Actual behavior

The array turns into its only child.

Environment data

Included in the repro steps.

Powershell Version

PS C:\Users\qisun\Downloads\LatestPowerShell> $PSVersionTable.PSVersion

Major Minor Patch Label
----- ----- ----- -----
    6     0     0 alpha

Have to mention that the issue does not repro when there are multiple children in that array.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions