Skip to content

ConvertFrom-JSON fails to parse syntactically-correct JSON array #3284

Closed

Description

Hello there,

Adopting a script from Windows to Linux here. The script generates a bunch of custom objects in array, which is converted to JSON using ConvertTo-JSON and written to file.

The resulting file is syntactically correct.

When loading the file and parsing it using ConvertFrom-JSON on Ubuntu, the loading fails with
ConvertFrom-JSON : Conversion from JSON failed with error: Unexpected end when reading JSON. Path '', line 1, position 5.

When the resulting file is stripped of character returns, the loading succeeds.

The process works great on Windows with or without carriage returns.

jsontest.zip

Steps to reproduce

jsontest.ps1:

$obj1 = New-Object PSCustomObject
Add-Member -InputObject $obj1 -NotePropertyName "objectName" -NotePropertyValue "object1Name"
Add-Member -InputObject $obj1 -NotePropertyName "objectValue" -NotePropertyValue "object1Value"

$obj2 = New-Object PSCustomObject
Add-Member -InputObject $obj2 -NotePropertyName "objectName" -NotePropertyValue "object2Name"
Add-Member -InputObject $obj2 -NotePropertyName "objectValue" -NotePropertyValue "object2Value"

$arr = @($obj1, $obj2)

$arr
$arr.count

$arr | ConvertTo-JSON | Out-File testjson.json -Encoding utf8

Produces the following output:

PS /home/administrator/Documents/> ./jsontest.ps1

objectName  objectValue
----------  -----------
object1Name object1Value
object2Name object2Value
2

Produces the following JSON:

[
    {
        "objectName":  "object1Name",
        "objectValue":  "object1Value"
    },
    {
        "objectName":  "object2Name",
        "objectValue":  "object2Value"
    }
]

Then other script wants to load the file and convert it back to JSON
Get-Content testjson.json | ConvertFrom-JSON

Expected behavior

PS /home/administrator/Documents/> Get-Content testjson.json | ConvertFrom-JSON

objectName  objectValue
----------  -----------
object1Name object1Value
object2Name object2Value

Actual behavior

PS /home/administrator/Documents/> Get-Content testjson.json | ConvertFrom-JSON

ConvertFrom-JSON : Conversion from JSON failed with error: Additional text encountered after finished reading JSON content: :. Path '', line 1, position 20.
At line:1 char:29
+ Get-Content testjson.json | ConvertFrom-JSON
+                             ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

After removing character returns

If I manually remove the character returns from JSON file:
[ { "objectName": "object1Name", "objectValue": "object1Value" }, { "objectName": "object2Name", "objectValue": "object2Value" }]
... then the ConvertFrom-JSON command works

Using -Compress

If I change the command from:
$arr | ConvertTo-JSON | Out-File testjson.json -Encoding utf8
to
$arr | ConvertTo-JSON -Compress | Out-File testjson.json -Encoding utf8
... the resulting JSON does not have carriage returns and subsequent loading works great.

Environment data

PS /home/administrator/Documents/>  lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.5 LTS
Release:        14.04
Codename:       trusty


PS /home/administrator/Documents/> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-alpha
PSEdition                      Core
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   3.0.0.0
GitCommitId                    v6.0.0-alpha.16
CLRVersion
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Activity

  1. danielodievich commented on Mar 8, 2017

    @danielodievich
    Author

    Seems related to #956

  2. modified the milestones: 6.0.0-beta1, on Mar 10, 2017
  3. heaths commented on May 10, 2017

    @heaths
    Contributor

    This occurs with program output as well. I was about to open a bug with the following information.

    Steps to reproduce

    1. Download recent https://github.com/Microsoft/vswhere release (or choco install vswhere)
    2. Run vswhere -format json | convertfrom-json

    Expected behavior

    Get back an array of zero or more instances. This works in PowerShell 5.1 (and in previous versions I've tried).

    Actual behavior

    Get an error like that in bug #956:

    > vswhere -format json | convertfrom-json
    convertfromjson : The term 'convertfromjson' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:24
    + vswhere -format json | convertfromjson | clip
    +                        ~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (convertfromjson:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Environment data

    > $PSVersionTable
    
    Name                           Value                                                                                   
    ----                           -----                                                                                   
    PSVersion                      6.0.0-beta                                                                              
    PSEdition                      Core                                                                                    
    BuildVersion                   3.0.0.0                                                                                 
    CLRVersion                                                                                                             
    GitCommitId                    v6.0.0-beta.1                                                                           
    OS                             Microsoft Windows 10.0.15063                                                            
    Platform                       Win32NT                                                                                 
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                 
    PSRemotingProtocolVersion      2.3                                                                                     
    SerializationVersion           1.1.0.1                                                                                 
    WSManStackVersion              3.0                                                                                     

    More information

    This does appear to be a multiline JSON issue like in bug #956 but that bug is indeed resolved. Both PowerShell 5 and 6 are measuring 33 lines on my machine from vswhere -format json so the behavior for returning an array from program output is fixed, but ConvertFrom-Json

  4. modified the milestones: , on May 15, 2017
  5. 4 remaining items

  6. Francisco-Gamino commented on May 25, 2017

    @Francisco-Gamino
    Contributor

    This issue has been addressed.

  7. prad9 commented on May 30, 2018

    @prad9

    Currently running 6.0.2 have same issue again.

  8. iSazonov commented on May 30, 2018

    @iSazonov
    Collaborator

    Pradeep Vattikuti (@prad9) The fix contains tests and the tests is passed. Could you please add more information and repo steps?

  9. kodeballer commented on Jul 4, 2018

    @kodeballer

    What OS Pradeep Vattikuti (@prad9) , clean powershell install ? Thanks

  10. prad9 commented on Jul 4, 2018

    @prad9

    Win 10 Business. I will try to re-install. Thanks

  11. miketheitguy commented on Jan 8, 2019

    @miketheitguy

    The behavior is confusing to use when combined with the get-content cmdlet.

    For example:
    Works
    get-content file.json | convertfrom-json
    Doesn't Work
    Convertfrom-Json -InputObject (Get-Content file.json)
    Works
    Convertfrom-Json -InputObject (Get-Content file.json -raw)

    When you do it via the latter method, you need to pass a -raw parameter because Get-Content automatically creates an array based on newlines. Still, seemingly very confusing as when piping to ConvertFrom-Json I don't need to use -raw.

  12. tadasu commented on Nov 29, 2024

    @tadasu

    Hi, another tiny json hits this issue. This file fails to convert even with the first syntax in #3284 (comment) .
    I assume that there are some problems processing unformatted (but syntactically correct) json with array.

    getmodule.txt
    psversiontable.txt
    repro.json

  13. DDHF22 commented on Aug 24, 2026

    @DDHF22

    Had the same error now with 7.6.5
    I mitigated it with Out-String like in
    sqlite3 -json db query | Out-String | ConvertFrom-Json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions