Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConvertFrom-Json fails to parse project.lock.json #1755

Closed
SteveL-MSFT opened this issue Aug 12, 2016 · 17 comments
Closed

ConvertFrom-Json fails to parse project.lock.json #1755

SteveL-MSFT opened this issue Aug 12, 2016 · 17 comments
Labels
Issue-Bug Issue has been identified as a bug in the product Resolution-Fixed The issue is fixed. WG-Cmdlets general cmdlet issues

Comments

@SteveL-MSFT
Copy link
Member

SteveL-MSFT commented Aug 12, 2016

$json = gc project.lock.json -Raw
ConvertFrom-Json $json

Expected: PowerShell should be able to parser the json data.
Actual: A useless error message which does not tell me what went wrong

ConvertFrom-Json : Cannot process argument because the value of argument "name" is not valid. Change the value of the
"name" argument and run the operation again.
At line:1 char:1
- ConvertFrom-Json $json
- ~~~~~~~~~~~~~~~~~~~~~~
  - CategoryInfo          : InvalidArgument: (:) [ConvertFrom-Json], PSArgumentException
  - FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
@SteveL-MSFT SteveL-MSFT added the WG-Cmdlets general cmdlet issues label Aug 12, 2016
@SteveL-MSFT SteveL-MSFT added this to the 6.0.0-Alpha.10 milestone Aug 12, 2016
@adityapatwardhan adityapatwardhan added Issue-Bug Issue has been identified as a bug in the product Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors labels Oct 11, 2016
@kittholland
Copy link
Contributor

kittholland commented Nov 26, 2016

Bug is due to empty name for json dictionary entry, which appears to be valid by json standards, issue I believe is when converting to psobject:

Example from the project.lock.json:

"projectFileDependencyGroups": {
"": [
"System.Management.Automation >= 6.0.0-*"
],

thrown from line 4474 in MshMemberInfo.cs

                if (String.IsNullOrEmpty(name))
                {
                    throw PSTraceSource.NewArgumentException("name");
                }

"Cannot process argument because the value of argument "name" is not valid. Change the value of the "name" argument and run the operation again."

Reproducible with the following simple json:

{
"": "Test"
}

I am uncertain how to proceed further to fix this issue, hopefully this helps.

@SteveL-MSFT
Copy link
Member Author

@kittholland thanks for looking into this, best as I can tell per http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf, an empty string as the name is allowed and works in javascript. I guess the next step is to determine if the issue is in newtonsoft json parser.

@kittholland
Copy link
Contributor

From what I could see in the debugger, newtonsoft was OK with it, but converting the Jobject into a PSObject failed because the name was empty.

@SteveL-MSFT
Copy link
Member Author

Ok, that makes sense (on why it failed). A HashTable will accept an empty string key, so it seems we should have PSObject support it as well. @kittholland are you going to work on a fix?

@kittholland
Copy link
Contributor

I have not looked deeply into what would be required, but I can give it a try.

kittholland added a commit to kittholland/PowerShell that referenced this issue Nov 29, 2016
In issue PowerShell#1755 we determined that you could not create a PSObject with a
key name of an empty string. This is supported in JSON and in
Hashtables.

Simple change was to lower evaluations of name field from
'IsNullOrEmpty' to '== null'.

Simple test to confirm is:
[pscustomobject]@{'' = 'Test'}

Simple JSON to test which results in ths same error:
{
"": "Test"
}

After these changes, both of these cases work and I am able to
succesfully Convert project.lock.json from JSON and back to JSON with no
data loss.
@joeyaiello
Copy link
Contributor

We should also handle the Invoke-WebRequest/Invoke-RestMethod scenario where you're able to post a null JSON key as a string or as an object.

@SteveL-MSFT SteveL-MSFT modified the milestones: 6.0.0-beta, 6.0.0 Feb 28, 2017
@SteveL-MSFT SteveL-MSFT modified the milestones: 6.0.0-beta2, 6.0.0-beta1 Mar 18, 2017
@SteveL-MSFT SteveL-MSFT modified the milestones: 6.0.0-beta, 6.0.0 May 15, 2017
@joeyaiello
Copy link
Contributor

@SteveL-MSFT I think we still need to fix this one for 6.0.0. Valid JSON should be parsed by ConvertFrom-Json.

@SteveL-MSFT SteveL-MSFT added this to the 6.1.0 milestone Sep 6, 2017
@SteveL-MSFT SteveL-MSFT removed this from the 6.0.0 milestone Sep 6, 2017
@kittholland
Copy link
Contributor

One other issue related to this is Invoke-Restmethod will silently fail and return null for valid json that is not PSCustomObject compatible.

This can be caused by NULL keys or case sensitive keys.

@SteveL-MSFT
Copy link
Member Author

@kittholland are you still working on this or should I take it?

@kittholland
Copy link
Contributor

@SteveL-MSFT I think I'm out of my depth on a solution for this. Thanks to whoever takes it from here.

@SteveL-MSFT SteveL-MSFT self-assigned this Oct 14, 2017
@SteveL-MSFT SteveL-MSFT removed the Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors label Oct 14, 2017
@SteveL-MSFT SteveL-MSFT modified the milestones: 6.1.0, 6.0.0-HighPriority Oct 14, 2017
@SteveL-MSFT SteveL-MSFT removed their assignment Oct 16, 2017
@SteveL-MSFT SteveL-MSFT modified the milestones: 6.0.0-HighPriority, 6.1.0 Oct 16, 2017
@SteveL-MSFT
Copy link
Member Author

Forgot about the discussion in #2820. Introducing a json specific type isn't happening in 6.0.0 timeframe.

@bergmeister
Copy link
Contributor

@SteveL-MSFT Can this issue be closed since PR #5043 provides a workaround via the -AsHashTable switch?

@SteveL-MSFT
Copy link
Member Author

@bergmeister agree!

@DarwinJS
Copy link
Contributor

DarwinJS commented Jun 7, 2018

Any work arounds to this without upgrading PowerShell?

@kittholland
Copy link
Contributor

@DarwinJS You can either modify your json to be compatible with PSObjects, or you will need to modify/create a cmdlet to convert from json into hashtables.

@bergmeister
Copy link
Contributor

@DarwinJS Since PowerShell Core is side by side and you can even get a self-contained version, you could just call into a newer version of pwsh from an old version of pwsh.

@DarwinJS
Copy link
Contributor

DarwinJS commented Jun 7, 2018

This is part of tooling that runs on thousands of machines I don't control and actually needs to be a single script with no public repo dependencies - so I resorted to the -replace method. Thanks.

@iSazonov iSazonov added the Resolution-Fixed The issue is fixed. label Jun 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug Issue has been identified as a bug in the product Resolution-Fixed The issue is fixed. WG-Cmdlets general cmdlet issues
Projects
Linux Distro/Mac Coverage
  
Awaiting triage
Development

No branches or pull requests

8 participants