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 and Test-Json accept non-compliant/non-standard JSON - no way to strictly validate JSON #10628
Comments
@chriskuech We is as simple as possible in Test-Json and delegate all to Json.NET. (NJsonSchema is used only if Schema parameter presents.) We call |
That makes sense, but I think it also makes sense for the default behavior of PowerShell to have lenient JSON parsing. Because the |
This is not a PowerShell feature to parse third-party languages. It can use features of .Net Core or another library like Json.NET. Moreover, JSON is too complicated for regex. |
Ugh, nested arrays seem doable with balancing group definitions, but I can't see how to validate the closing braces of nested objects at the same time. |
A Trying to roll our own strict mode seems like the wrong way to go, though, as @iSazonov states. Unfortunately, the Json.Net author in the past has expressed no interest in (opt-in) strict parsing; see this closed issue from 2017: JamesNK/Newtonsoft.Json#1162 |
Would the strict validator be required to reject:
If it is not required to reject those things, then it is simpler to implement and faster to run, I think. |
.Net Core team actively works on new JSON API implementation to replace Json.Net. All new features will be in .Net Core. Open feature request issues in CoreFX repo if you want more features - we will get them. |
System.Text.Json currently seems focused on UTF-8 byte sequences. It seems a bit wasteful if TestJsonCommand has to convert |
@KalleOlaviNiemitalo It is a question for .Net Core team too. |
This issue has been marked as external and has not had any activity for 1 day. It has been be closed for housekeeping purposes. |
When are you planning to solve this?! E.g. I want to validate my Azure ARM-Templates (json) using Test-Json |
I re-open so that we could look this after moving to new .Net Json API. If you have an interest you could create a simple C# app to test new API and report to .Net Runtime repo. This will increase our chances of getting a solution faster. |
#11397 will resolve this. |
Can anyone update the status of this issue as it is unclear to me whether the above mentioned #11397 resolved the issue as currently our Test-Json is still showing True for JSON that contains a trailing comma. (PowerShell 7.2.6 latest) I need a solid solution to validate JSON input in my DevOps pipelines so any human error can be excluded from the automated processes. |
For reference #18023 |
The JSON Schema implementation has been recently updated to JsonSchema.Net and is available in the latest Powershell snapshot. As such, the parser is now powered by System.Text.Json. Any JSON parsing issues should be handled by that. Please retest this to see if it is still a problem. |
Hit this today under PowerShell 7.3.8. |
#18141 was the merged PR for using System.Text.Json. It's included in PowerShell 7.4.0-preview.4. I don't expect that it will be backported to PowerShell 7.3. |
Only
What I think should have happened - so as to preserve backward compatibility - is the introduction of, say, a The v7.4.0 |
Summary of the new feature/enhancement
There does not seem to be any way to validate whether a JSON string is valid, which makes it impossible to use PowerShell to validate JSON before passing it to another application.
For example, the following JSON is invalid due to the trailing comma, but
Test-Json
still returns$true
.I propose adding a
-Strict
parameter toTest-Json
for testing that the JSON string adheres strictly to the JSON specification.Side note
I was very excited when I found the
Test-Json
cmdlet, but so far every use case it would have helped, it has not behaved as expected. See also #9560. If this cannot be implemented using the current library, please strongly consider using Json.NET instead of NJsonSchema.The text was updated successfully, but these errors were encountered: