ConvertFrom-Json "[]" incorrectly returns $null instead of an empty array
See original GitHub issueSteps to reproduce & Expected results & Actual results
(ConvertFrom-Json "[]") -eq @()
#return False should return True
(ConvertFrom-Json "[]") -eq $null
# => returns True, should return False
Comment
ConvertFrom-Json on Windows PSVersion = 5.1.18362.752 returns the correct value
Environment data
$PSVersionTable
Name Value
---- -----
PSVersion 7.1.0-preview.6
PSEdition Core
GitCommitId 7.1.0-preview.6
OS Darwin 19.6.0 Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.14…
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top Results From Across the Web
PowerShell ConvertFrom-Json with OData returns empty ...
In your example, the only problem is write-host seems to output the object array as null, which seems like a bug or "suboptimal...
Read more >ConvertFrom-Json - PowerShell
When the input object is an empty string, ConvertFrom-Json doesn't generate any output. The InputObject value can't be $null . Type: String. Position:...
Read more >Why does Powershell silently convert a string array with ...
Evidently, PowerShell implicitly 'unboxes' a single-item array to a single object,. And zero item results to $null .
Read more >Powershell returns null when converting to json
I found this workaround on reddit. First convert it to CSV, then to Json. And you get the string. Code: Select all. Get-VBRJob...
Read more >Return json empty. var jsonString = JsonConvert. ...
Add (new NullJsonHandler ()); Now all the Asp. the json varaible only contains an empty array, so what is passed to the api,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This is the most unexpected behaviour I think I’ve seen so far in Powershell…
When converting from an empty array you’d always expect an empty array… 🤔
The
ConvertFrom-Jsondefault behavior was changed in v7 to exhibit the typical PowerShell behavior of enumerating arrays in the pipeline; thus, an empty array sends nothing through the pipeline (see #3424).If you want to preserve a parsed-from-JSON array, use the
-NoEnumerateswitch: