Community
In the following example, an error happens.
```powershell
$Google = Invoke-WebRequest 'google.com'
$Google | ConvertTo-Json
```
> ConvertTo-Json: An item with the same key has already been added. Key: Content
Why?
On GitHub, [PowerShell/issues/11910](https://github.com/PowerShell/PowerShell/issues/11910) shows us the same error message, but doesn't currently contain a verbose explanation.
Briefly: an object may inherit properties from a parent type, but if it attempts to overload a property that exists on the parent, then it cannot be seamlessly serialised by the ConvertTo-Json cmdlet.
Top Answer
example
PeterVandivier
[The output type of Invoke-WebRequest][Invoke-WebRequest#output] is the child class [BasicHtmlWebResponseObject] which inherits from parent class [WebResponseObject]. A close comparison of those docs pages will reveal that many properties are inherited from parent to child, but that `Content` is _not_ inherited; instead - it is declared a second time. Not only this, but a deeper examination of the parent type will reveal that it is declared as a _different type_ in the child class than that of the same-named property in the parent type. Compare:
[WebResponseObject.Content]
```cs
public byte[] Content { get; protected set; }
```
[BasicHtmlWebResponseObject.Content]
```cs
public new string Content { get; private set; }
```
This is notable, because if we take the example from [issues/11910] and modify it so that the overloaded Property is of the same type...
```diff
$type = @"
public class B
{
public int A { get; set; }
}
public class C : B
{
- public new string A { get; set; }
+ public new int A { get; set; }
}
"@
```
...then serialisation of on object of class `C` does not error.
```powershell
Add-Type $type
$obj = New-Object C
$obj | ConvertTo-Json
```
[Invoke-WebRequest#output]: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest#outputs
[BasicHtmlWebResponseObject]: https://docs.microsoft.com/en-us/dotnet/api/microsoft.powershell.commands.basichtmlwebresponseobject
[WebResponseObject]: https://docs.microsoft.com/en-us/dotnet/api/microsoft.powershell.commands.webresponseobject
[issues/11910]: https://github.com/PowerShell/PowerShell/issues/11910
[WebResponseObject.Content]: https://github.com/PowerShell/PowerShell/blob/c146ff7/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebResponseObject.Common.cs#L24
[BasicHtmlWebResponseObject.Content]: https://github.com/PowerShell/PowerShell/blob/c146ff7/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs#L66
Answer #2
example
PeterVandivier
A trivial workaround exists: you can pipe the object through `Select *` which clobbers the strict typing.
```powershell
PS> $Google.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False BasicHtmlWebResponseObject Microsoft.PowerShell.Commands.WebResponseObject
PS> ($Google | select *).GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False PSCustomObject System.Object
PS>
```
Once the object is freed of the constraints of its inheritence, then you may serialise it without error.
```powershell
($Google | select *) | ConvertTo-Json
```
---
HT to [@chrisdent on powershell slack](https://powershell.slack.com/archives/C1RCWRDL4/p1606739422257800) for the assist :-)