Skip to content
Advertisement

C#: Dapper with JsonConvert.SerializeObject() not working properly

I’m new to Newtonsoft.Json and Dapper.

I am executing an SQL query, and using the query’s result I’m converting it to a JSON string to try to make it look like this:

However when I run my C# code, my output looks completely different along with some unexpected additions:

As you can see I do not understand why it is adding the additional “JSON_F52E2B61-18A1-11d1-B105-00805F49916B”. How do I remove these? How do I change my code to make it look like my desired output json string?

This is my code. I also made a fiddle with the incorrect output I’m getting https://dotnetfiddle.net/uWV6vs :

Advertisement

Answer

I believe you don’t need to request JSON from SQL, Dapper will parse results to the objects automatically

Removing “FOR JSON PATH, root (‘Orders’), INCLUDE_NULL_VALUES” should help

UPDATE:

sorry, keep updating the answer. This one gives you objects with the right structure and no extra backslashes

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement