Skip to content

ConvertFrom-Json flattens single-item arrays into objects when opening brace of JSON isn't on first line #18804

Closed

Description

Prerequisites

Steps to reproduce

When using ConvertFrom-Json to convert a JSON file to a PSCUstomObject, I have run into a weird behavior when the JSON's main object doesn't start on line 1, but instead starts either with a blank line, or a comment (In this case I put a comment there to prevent any trim function to strip the newline, but the result is the same whether or not the first line is a comment or just blank, as long as it isn't {

Take the following JSON file, test.json in the behavior examples below:

// This file contains test data
{
  "testArray": [
    {
      "name": "test"
    }
  ],
  "testArray2": [
    {
      "name": "test2"
    },
    {
      "name": "test3"
    }
  ]
}

And process it with ConvertFrom-Json, you'd expect it to be the same as when processing the following file:

{
  "testArray": [
    {
      "name": "test"
    }
  ],
  "testArray2": [
    {
      "name": "test2"
    },
    {
      "name": "test3"
    }
  ]
}

But in the first case, testArray is represented as a PSCUstomObject, not an array of objects.

Expected behavior

PS> $testContent = Get-Content test.json | ConvertFrom-Json

PS> $testContent.testArray.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

Actual behavior

PS> $testContent = Get-Content test.json | ConvertFrom-Json

PS> $testContent.testArray.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    PSCustomObject                           System.Object

Error details

No response

Environment data

PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.1
PSEdition                      Core
GitCommitId                    7.3.1
OS                             Microsoft Windows 10.0.22623
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Activity

  1. mklement0 commented on Dec 15, 2022

    @mklement0
    Contributor

    You're seeing the results of an (unfortunate) heuristic built into ConvertFrom-Json, as previously discussed in:

    To avoid the problem, use Get-Content -Raw in order to pass the entire document as a single string, which not only avoids the ambiguity, but speeds up your command as well.

  2. added and removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Mar 20, 2023
  3. ghost closed this as completedon Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions