Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

13
  • How exactly are you calling it in PowerShell? Maybe you are doing the same there - double-decoding it, once by PowerShell itself by using Invoice-RestMethod which already handles JSON for you (unlike Invoke-WebRequest) and once by another manual call to ConvertFrom-Json? Commented Apr 1, 2020 at 12:45
  • And I'd guess that you have the same issue on the server side - once it's already getting encoded automatically (since ASP.NET will automatically return the right format based on content negotiation, defaulting to JSON if no supported Accept header was sent), and maybe you additionally call JsonSerializer.Serialize and encode it a second time... Commented Apr 1, 2020 at 12:46
  • 1
    I assume what happens is this: You have an object, you call JsonSerializer.serialize on it, turning it into a string. Then you return it, and ASP.NET JSON-encodes your string, resulting in a double-encoded object string. Invoke-RestMethod receives that string and decodes it, resulting in the original once-encoded string representation of the object, and you then call ConvertFrom-Json to resolve that second layer of encoding, getting back the original object. Commented Apr 1, 2020 at 12:49
  • 1
    I'm definitely not calling a 2nd Serialize << from your perspective it'd be the 1st - which is apparent from your stating that it is returning a string already! It is returning it to ASP.NET which will nicely encode it for you to send your value as JSON... Which already was JSON before, since you serialized it manually (unnecessarily). That's what I think Commented Apr 1, 2020 at 12:50
  • 1
    To sum up what I was trying to say: ASP.NET auto-encodes for you, and Invoke-RestMethod auto-decodes for you. So no need to manually encode or decode anything, otherwise it ends up getting encoded twice, which seems to be what happened here. You can just return an object. - I'm pretty convinced now that this is what's going on, so I'll turn it into an answer Commented Apr 1, 2020 at 12:53