You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Test-Json doesn't recognize JSON arrays or primitives #11384
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues
Michael Klement (@mklement0) Could you please add more info why it should work in the way? Maybe reference to a standard or a quote from the standard. It would help to avoid regressions in follow fix.
We use NewtonSoft JObject.Parse() which throws. I tried System.Text.Json.JsonDocument.Parse() and … most of our tests failed because examples in the issue and out tests in Test-Json.Tests.ps1 use single quotes but Json standard allows only double quotes.
So we should decide what to do because switching to JsonDocument.Parse() looks like a breaking change.
With double quotes the tests and the issue examples are passed if JsonDocument.Parse() is used.
Michael Klement (@mklement0) Thanks! I think we should move to Core API if possible. Sooner or later it will happen anyway. If there are no serious problems, it is better to do it "sooner".
Reacted by Michael Klement and Rain Sallow (/u/ta11ow)
PR #11397 will implicitly fix these issues, if I understand correctly, but due to its move to a different underlying API it will bring breaking changes, notably no longer being able to use single-quoted JSON (right, Ilya (@iSazonov)?).
I wasn't even aware of the other issue that Test-Json doesn't support its input as an array of lines, the way ConvertFrom-Json does (e.g., '{ "foo": ', '"bar" }' | Test-Json fails, which succeeds with ConvertFrom-Json)
As for a workaround: It's sufficient to use only ConvertFrom-Json, and derive from its failure whether the input is valid JSON or not:
We have PRs for ConvertFrom-Json and Test-Json a long time without review. Unfortunately, we missed this time. I think it makes no sense to continue this now. .Net 5 will be frozen soon for release and we have no chance to fix something there if we find problems.
This basically means that if you want to do anything more complicated than a single object you have to break the array into unique objects, which is terrible from a performance perspective, and otherwise you're stuck with try-catch stuff.
I just stumbled across this issue and am baffled that it hasn't been resolved in 2 years, even in the latest versions of PowerShell.
I am currently running PowerShell 7.2 in Visual Studio Code and Test-Json reports a JSON file is invalid with an array as the root object. Both Visual Studio Code and ConvertFrom-Json treat this file as valid JSON.
As others have mentioned, regardless of what JSON standard PowerShell is conforming to, Test-Json and ConvertFrom-Json should treat the same input as either valid or invalid JSON. They should not give different results.
In my case, I can change the schema of my source config file, but that's a lame workaround for a 2-year-old bug.
Really frustrating that this issue still exists. I have exactly a case for which I can not develop a workaround and Test-Json cmdlet should really have this issue fixed.
Anybody have a working solution when you have to really test against a json schema ?
Yes, this is unbelievable and frustrating, that such a simple and obvious error is still not fixed.
I ended up writing my own Test-Json script. Unfortunately I can't share it, as it's now the property of my employer.
Looking at the original code it's not that hard to write your own. You can safely assume, that Newtonsoft.Json and NJsonSchema libraries are present, as PowerShell uses them for this crippled implementation, so they are a mandatory dependency. So you can parse your Json using Newtonsoft.Json, parse your schema using NJsonSchema and do the validation of the first by the second, so using the same libraries the Test-Json command does and probably a similar approach.
You can add your own features too, e.g. accept Json file paths (not just Json content) and maybe with wildcards, load Json schema indicated in the Json document, provide a way to add PowerShell code validation (e.g. where Json schema can't cover some requirements), change the output format (so you can have results for multiple Json files and it's clear which error is for which document), allow some deserialization options to be set (like max depth, date or number processing), the parsed Json to be returned (so you do not have to parse it separately), etc.
Most of this can be done while maintaining compatibility with the Test-Json command (i.e. it failing for non-objects), by having the needed behavior work on switches and keeping the default call perform the same crap as Test-Json.
No idea why Ilya (@iSazonov) 's MR was closed, but per Michael Klement (@mklement0) 's suggestion, I just changed the parsedJson from JObject to JToken and everything works with just one line of diff (plus one to remove unnecessary outside declaration):
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues
Activity
iSazonov commentedon Dec 18, 2019
Michael Klement (@mklement0) Could you please add more info why it should work in the way? Maybe reference to a standard or a quote from the standard. It would help to avoid regressions in follow fix.
mklement0 commentedon Dec 18, 2019
Ilya (@iSazonov), I've added a link to json.org to the OP, and also a demonstration that
ConvertFrom-Jsonitself happily accepts such inputs.iSazonov commentedon Dec 18, 2019
We use NewtonSoft JObject.Parse() which throws. I tried System.Text.Json.JsonDocument.Parse() and … most of our tests failed because examples in the issue and out tests in Test-Json.Tests.ps1 use single quotes but Json standard allows only double quotes.
So we should decide what to do because switching to JsonDocument.Parse() looks like a breaking change.
With double quotes the tests and the issue examples are passed if JsonDocument.Parse() is used.
mklement0 commentedon Dec 18, 2019
The single quotes aren't in the data, they are only there as PowerShell's string delimiter.
What
Test-Jsonsees is just42and[ 1, 2 ]and both are valid JSON strings.I haven't looked at the internals, but it's obvious that whatever
ConvertFrom-Jsonmanages to parse should be considered valid byTest-Json.jochenvanwylick commentedon Dec 19, 2019
+1 Encountered the same problem here - would love to see this resolved!
iSazonov commentedon Dec 19, 2019
jochenvanwylick You could download artifact from #11397, check your scenarios and report issues there.
mklement0 commentedon Dec 19, 2019
Ilya (@iSazonov): In the current implementation,
PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs
Line 70 in 7b33cfe
should be:
Only
JTokencorrectly parses JSON documents such as42and[ 1, 2 ], whereasJObjectaccepts just a single object (dictionary).Or, better yet, use the same technique that
ConvertFrom-Jsonuses:PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs
Lines 179 to 187 in 7b33cfe
Note that the code there contains a workaround for a NewtonSoft bug that's no longer needed.
In a
System.Text.Json-based implementation,System.Text.Json.JsonDocument.Parse()is indeed the right tool.All these tests succeed:
iSazonov commentedon Dec 19, 2019
Michael Klement (@mklement0) Thanks! I think we should move to Core API if possible. Sooner or later it will happen anyway. If there are no serious problems, it is better to do it "sooner".
thedavecarroll commentedon Mar 11, 2020
Has there been any progress on this?
A Redditor recently made a similar comment. I responded with a link to this issue.
Basically, I said that they could use
ConvertFrom-Json | ConvertTo-Json | Test-Jsonto perform tests.Is this the best advice for PowerShell 7 GA?
mklement0 commentedon Mar 11, 2020
Dave Carroll (@thedavecarroll), yes, it's unfortunate that this wasn't addressed in time for 7.0
PR #11397 will implicitly fix these issues, if I understand correctly, but due to its move to a different underlying API it will bring breaking changes, notably no longer being able to use single-quoted JSON (right, Ilya (@iSazonov)?).
I wasn't even aware of the other issue that
Test-Jsondoesn't support its input as an array of lines, the wayConvertFrom-Jsondoes (e.g.,'{ "foo": ', '"bar" }' | Test-Jsonfails, which succeeds withConvertFrom-Json)As for a workaround: It's sufficient to use only
ConvertFrom-Json, and derive from its failure whether the input is valid JSON or not:iSazonov commentedon Mar 11, 2020
Michael Klement (@mklement0) Yes.
Currently we are waiting .Net 5.0 public preview to continue.
dgard1981 commentedon Jul 8, 2020
Might we get another update on this issue?
Ilya (@iSazonov) said -
Looks like this has now happened.
With regards to suggested workarounds using
ConvertFrom-Json- unfortunately that's no good if you are trying to test against a schema.iSazonov commentedon Jul 8, 2020
We have PRs for ConvertFrom-Json and Test-Json a long time without review. Unfortunately, we missed this time. I think it makes no sense to continue this now. .Net 5 will be frozen soon for release and we have no chance to fix something there if we find problems.
8 remaining items
JordanTheITGuy commentedon Dec 29, 2021
This is pretty frustrating.
This basically means that if you want to do anything more complicated than a single object you have to break the array into unique objects, which is terrible from a performance perspective, and otherwise you're stuck with try-catch stuff.
traberc commentedon Jan 15, 2022
I just stumbled across this issue and am baffled that it hasn't been resolved in 2 years, even in the latest versions of PowerShell.
I am currently running PowerShell 7.2 in Visual Studio Code and
Test-Jsonreports a JSON file is invalid with an array as the root object. Both Visual Studio Code andConvertFrom-Jsontreat this file as valid JSON.As others have mentioned, regardless of what JSON standard PowerShell is conforming to,
Test-JsonandConvertFrom-Jsonshould treat the same input as either valid or invalid JSON. They should not give different results.In my case, I can change the schema of my source config file, but that's a lame workaround for a 2-year-old bug.
m-soltani commentedon Mar 12, 2022
Really frustrating that this issue still exists. I have exactly a case for which I can not develop a workaround and
Test-Jsoncmdlet should really have this issue fixed.Anybody have a working solution when you have to really test against a json schema ?
matyrr-havok commentedon Mar 14, 2022
If you're desperate, you could try the hacky code in my comment above. It seemed to work OK for a scenario I had.
hilari0n commentedon Mar 14, 2022
Yes, this is unbelievable and frustrating, that such a simple and obvious error is still not fixed.
I ended up writing my own Test-Json script. Unfortunately I can't share it, as it's now the property of my employer.
Looking at the original code it's not that hard to write your own. You can safely assume, that Newtonsoft.Json and NJsonSchema libraries are present, as PowerShell uses them for this crippled implementation, so they are a mandatory dependency. So you can parse your Json using Newtonsoft.Json, parse your schema using NJsonSchema and do the validation of the first by the second, so using the same libraries the Test-Json command does and probably a similar approach.
You can add your own features too, e.g. accept Json file paths (not just Json content) and maybe with wildcards, load Json schema indicated in the Json document, provide a way to add PowerShell code validation (e.g. where Json schema can't cover some requirements), change the output format (so you can have results for multiple Json files and it's clear which error is for which document), allow some deserialization options to be set (like max depth, date or number processing), the parsed Json to be returned (so you do not have to parse it separately), etc.
Most of this can be done while maintaining compatibility with the Test-Json command (i.e. it failing for non-objects), by having the needed behavior work on switches and keeping the default call perform the same crap as Test-Json.
m-soltani commentedon Mar 28, 2022
puhh, dont want to waste time, mess around with internals of PowerShell. Found a workaround for this.
eric-ash-1 commentedon May 5, 2022
I would also love to see this bug fixed. It's still there in 7.2.3
dkaszews commentedon Jul 20, 2022
No idea why Ilya (@iSazonov) 's MR was closed, but per Michael Klement (@mklement0) 's suggestion, I just changed the
parsedJsonfromJObjecttoJTokenand everything works with just one line of diff (plus one to remove unnecessary outside declaration):I'll add extra tests and create an MR.