question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ConvertFrom-Json "[]" incorrectly returns $null instead of an empty array

See original GitHub issue

Steps 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
ElvenSpellmakercommented, Mar 28, 2021

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… 🤔

2reactions
mklement0commented, Sep 8, 2020

The ConvertFrom-Json default 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 -NoEnumerate switch:

# Output is, loosely speaking, $null
# (technically,  [System.Management.Automation.Internal.AutomationNull]::Value)
PS> (ConvertFrom-Json "[]") -is [array]  
False

# -NoEnumerate sends the (empty) array as-is, as whole to the pipeline.
PS> (ConvertFrom-Json "[]" -NoEnumerate) -is [array]
True
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found