Repository navigation
ConvertFrom-JSON fails to parse syntactically-correct JSON array #3284
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.
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
danielodievich commentedon Mar 8, 2017
Seems related to #956
heaths commentedon May 10, 2017
This occurs with program output as well. I was about to open a bug with the following information.
Steps to reproduce
choco install vswhere)vswhere -format json | convertfrom-jsonExpected 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:
Environment data
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 jsonso the behavior for returning an array from program output is fixed, butConvertFrom-Json4 remaining items
Francisco-Gamino commentedon May 25, 2017
This issue has been addressed.
prad9 commentedon May 30, 2018
Currently running 6.0.2 have same issue again.
iSazonov commentedon May 30, 2018
Pradeep Vattikuti (@prad9) The fix contains tests and the tests is passed. Could you please add more information and repo steps?
kodeballer commentedon Jul 4, 2018
What OS Pradeep Vattikuti (@prad9) , clean powershell install ? Thanks
prad9 commentedon Jul 4, 2018
Win 10 Business. I will try to re-install. Thanks
miketheitguy commentedon Jan 8, 2019
The behavior is confusing to use when combined with the
get-contentcmdlet.For example:
Works
get-content file.json | convertfrom-jsonDoesn'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
-rawparameter becauseGet-Contentautomatically creates an array based on newlines. Still, seemingly very confusing as when piping toConvertFrom-JsonI don't need to use-raw.tadasu commentedon Nov 29, 2024
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
DDHF22 commentedon Aug 24, 2026
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