-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
Add Parameter to ConvertTo-Json to ignore unsupported properties #5749
Comments
@mike-the-automator Does |
Thanks for your response. I tested Transcript:
In the scenario I envision, when the new parameter is supplied there would be no error thrown and the value of the variable |
@bergmeister I have mulled over the suggestion to just change the behavior for Also, silently discarding data without being explicitly told to do so feels wrong. It almost feel more like an occasion to use Another means to get the result I want would be to just create a cmdlet to strip non-JSON compliant fields of an object that would result in this error and put it in front of ConvertTo-Json in the pipeline, e.g.
I could always create a convenience wrapper-function to eliminate the need for a long pipeline (I suck at naming things). Given that perspective I can understand if the consensus is that the complexity/utility ratio doesn't pan out for this feature request, but I'd like to know what others think. |
I think it is a separate issue that ErrorAction is ignored and that it still raises an exception, but I think the correct functionality of ErrorAction with SilentlyContinue should be to return no data, not to strip out some data. I think either Force or SkipIncompatibleProperties would be a better solution as you are explicitly telling it to get rid of data. |
@FireInWinter Created a separate issue as requested. I think we are in agreement that the behavior requested in this issue should be implemented as a new parameter for the reasons already stated. I'm interested in contributing and would be happy to work on a PR for either issue, but I'd rather not invest time in this feature request if it has a low probability of being merged. I'm not asking for a guarantee that my PR will be accepted, but it would be nice to know if I'm barking up the wrong tree here. |
My preference would be to have a |
Generally, the use case you're describing is better handled using serialization based on CLIXML rather than JSON. What makes this more cumbersome is the fact that you must export to a (temporary) file first, using |
@mklement0 agree that if the use case is to serialize and deserialize later without care of the serialization format, CliXml is a better solution. However, I can see cases where you want specifically json (more human readable than xml...) |
I merely restricted my comment to pointing out the CLIXML alternative, because I don't know what the right answer to the specific problem at hand is, but here are a few thoughts: A simpler way to reproduce the problem, using a hashtable with a non-string key: @{ 1 = 'one' } | ConvertTo-Json The error message suggests that an explicit decision was made not to support non-string keys and to fail in that event. The alternatives - to be activated via new switch(es) - are:
Given that JSON is not suitable for general-purpose serialization, however, I wonder if this is really necessary. |
I wonder too. |
Let's not let convenience become irrelevant in this project. I have use cases for this myself that I have had to work around. I have object |
This reminds me that in my HttpListener module, I had to work around JSON serialization limitations by manually stripping types that were not supported. |
I would suggest a function which converts all none-string values to string values before piping it to convertto-json. @{ 1 = 'one' }.getenumerator() | select key,value | ConvertTo-Json |
This issue is about ignoring values that can't be converted to JSON. By contrast, you're looking to for an opt-in method for including such values by converting dictionary keys to strings, so I suggest you create a new issue with your proposal. As an aside, re your example: It isn't the |
Greeting from 5 years later where I need this functionality in a PowerShell Azure Function. Specifically I'm trying to convert the output of "Get-Error" to JSON to push it to a logging endpoint. Fails with |
Would like to see this functionality as well. Need it in an Azure Pipeline for a similar scenario as @heinrich-ulbricht, to convert an error record to JSON for consumption further in the pipeline. |
|
+1 for a parameter to |
I understand the need of a solution for the fact that a single item/property causes the whole [PSCustomObject]@{ 1 = 'one' } | ConvertTo-Json
{
"1": "one"
} And would of cause fail for something like: [PSCustomObject]@{ 1 = 'one'; '1' = 'one2' } | ConvertTo-Json
ParserError:
Line |
1 | [PSCustomObject]@{ 1 = 'one'; '1' = 'one2' } | ConvertTo-Json
| ~~~
| Duplicate keys '1' are not allowed in hash literals. but that is the actual error I would like to be able suppress... |
Something strange happens the other way around:
Or:
So the first key will be overwritten by the second. No errors...? I would prefer an error, because the JSON is incorrect and the conversion should throw an error? I did not expect this without an error. Another experiment:
Gives:
And:
Gives:
Converting it to JSON:
Gives 2 Key Value pairs
And this works too:
Gives:
|
Has anyone asked why ConvertTo-Json considers a ListDictionaryInternal as invalid? It's entirely possible to convert the keys in a ListDictionaryInternal to JSON if GetEnumerator() is run on it first. Transcript: PS>1 / 0 RuntimeException: Attempted to divide by zero.
ConvertTo-Json: The type 'System.Collections.ListDictionaryInternal' is not supported for serialization or deserialization of a dictionary. Keys must be strings.
ConvertTo-Json: The type 'System.Collections.ListDictionaryInternal' is not supported for serialization or deserialization of a dictionary. Keys must be strings. PS>$e.Exception.Data.GetEnumerator() | % { $_ | ConvertTo-Json -Depth 2 } | Out-Null |
This will give some insight and possibly a solution: |
I'd like to propose an additional parameter for the ConvertTo-Json cmdlet that specifies the caller would like the cmdlet to ignore properties of the InputObject that cannot be converted to JSON successfully. Currently, the behavior of the cmdlet is to throw an exception with a message similar to the following
"ConvertTo-Json : The type 'System.Collections.ListDictionaryInternal' is not supported for serialization or "deserialization of a dictionary. Keys must be strings.
My proposal is that if the
-SkipIncompatibleProperties
(parm name feedback appreciated :-) ) parameter is specified, then the cmdlet would silently skip over these properties and not throw any exceptions.I've been frustrated by this error several times in my scripting in situations where I don't really care about the particular property that triggered the error; I would just like a quick and easy serialized representation of the object. In the most recent instance, I needed a way to record exception info in a VARCHAR column in SQL Server. The property of the System.Exception that triggered this error was the "Data" field which I didn't really care about, but it prevented me from easily using the cmdlet to serialize the exception object.
As an aside, it looks like there is a proposal by @KirkMunro to move the JSON cmdlets to their own module. I understand it may make sense to wait on this feature until the module split occurs.
The text was updated successfully, but these errors were encountered: