10000 ConvertFrom-JSON fails to parse syntactically-correct JSON array · Issue #3284 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content
ConvertFrom-JSON fails to parse syntactically-correct JSON array #3284
@danielodievich

Description

@danielodievich

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

Metadata

Metadata

Labels

Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0