Deserializing a json string into structure results in exception2019 Community Moderator ElectionC# Could not Cast or Convert System.String to Class objectWhat is the difference between String and string in C#?Safely turning a JSON string into an objectSerializing to JSON in jQueryCan comments be used in JSON?How can I pretty-print JSON in a shell script?Case insensitive 'Contains(string)'What is the correct JSON content type?Why does Google prepend while(1); to their JSON responses?Deserialize JSON into C# dynamic object?Jackson with JSON: Unrecognized field, not marked as ignorable

How does NOW work?

What was the Kree's motivation in Captain Marvel?

Are there historical instances of the capital of a colonising country being temporarily or permanently shifted to one of its colonies?

Difference on montgomery curve equation between EFD and RFC7748

What's wrong with this bogus proof?

Plausibility of Mushroom Buildings

Database Backup for data and log files

Do I really need to have a scientific explanation for my premise?

How can I get players to stop ignoring or overlooking the plot hooks I'm giving them?

What problems would a superhuman have whose skin is constantly hot?

Can I pump my MTB tire to max (55 psi / 380 kPa) without the tube inside bursting?

Motivation for Zeta Function of an Algebraic Variety

Error during using callback start_page_number in lualatex

Reverse string, can I make it faster?

How strictly should I take "Candidates must be local"?

How to draw cubes in a 3 dimensional plane

List elements digit difference sort

Accountant/ lawyer will not return my call

What Happens when Passenger Refuses to Fly Boeing 737 Max?

What is the magic ball of every day?

Books that are narrated using various points of view of the main characters

Virginia employer terminated employee and wants signing bonus returned

How to fix the Unknown Command error caused by starting CmdInit.cmd using TCC/LE?

NASA's RS-25 Engines shut down time



Deserializing a json string into structure results in exception



2019 Community Moderator ElectionC# Could not Cast or Convert System.String to Class objectWhat is the difference between String and string in C#?Safely turning a JSON string into an objectSerializing to JSON in jQueryCan comments be used in JSON?How can I pretty-print JSON in a shell script?Case insensitive 'Contains(string)'What is the correct JSON content type?Why does Google prepend while(1); to their JSON responses?Deserialize JSON into C# dynamic object?Jackson with JSON: Unrecognized field, not marked as ignorable










1















I'm having issues mapping a json string returned from sql.



The structure of the object looks like this



 public class OrderSummaryDto

public List<PrescriptionInfo> PrescriptionsInfo get; set;

public class PrescriptionInfo

public string Name get; set;
public AdministrationInfo Administration get; set;

public class AdministrationInfo

public string Instructions get; set;
public string PackageType get; set;





The json string returned from DB



["Name":"testName","Units":3,"Medium":"flower power","Administration":""Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case"","Name":"testName","Units":3,"Medium":"flower power","Administration":""Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case""]


The Sql that generates the json string



(SELECT _co.[Name] 
,_co.[Pharmacy_Instructions] AS [Administration]
FROM [dbo].[Compounds] _co
WHERE _co.[Id] = 1
FOR JSON PATH) AS [PrescriptionsInfo]


Pharmacy_Instructions is already a JSON formated string




Message "Could not cast or convert from System.String to
Models.Order.Summary.OrderSummaryDto+PrescriptionInfo+AdministrationInfo." string











share|improve this question

















  • 2





    related: stackoverflow.com/questions/40804648/…

    – Meg
    Mar 6 at 15:15






  • 4





    Administration appears to have been double serialized as evident by "{"Instructions"....

    – Nkosi
    Mar 6 at 15:15











  • your json string seems to be an array of PrescriptionsInfo not a OrderSummaryDto

    – tschmit007
    Mar 6 at 15:18











  • Out of curiosity: why not use a (micro) ORM such as Dapper make your quries return strongly typed objects? :-)

    – Bo Mortensen
    Mar 6 at 15:20















1















I'm having issues mapping a json string returned from sql.



The structure of the object looks like this



 public class OrderSummaryDto

public List<PrescriptionInfo> PrescriptionsInfo get; set;

public class PrescriptionInfo

public string Name get; set;
public AdministrationInfo Administration get; set;

public class AdministrationInfo

public string Instructions get; set;
public string PackageType get; set;





The json string returned from DB



["Name":"testName","Units":3,"Medium":"flower power","Administration":""Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case"","Name":"testName","Units":3,"Medium":"flower power","Administration":""Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case""]


The Sql that generates the json string



(SELECT _co.[Name] 
,_co.[Pharmacy_Instructions] AS [Administration]
FROM [dbo].[Compounds] _co
WHERE _co.[Id] = 1
FOR JSON PATH) AS [PrescriptionsInfo]


Pharmacy_Instructions is already a JSON formated string




Message "Could not cast or convert from System.String to
Models.Order.Summary.OrderSummaryDto+PrescriptionInfo+AdministrationInfo." string











share|improve this question

















  • 2





    related: stackoverflow.com/questions/40804648/…

    – Meg
    Mar 6 at 15:15






  • 4





    Administration appears to have been double serialized as evident by "{"Instructions"....

    – Nkosi
    Mar 6 at 15:15











  • your json string seems to be an array of PrescriptionsInfo not a OrderSummaryDto

    – tschmit007
    Mar 6 at 15:18











  • Out of curiosity: why not use a (micro) ORM such as Dapper make your quries return strongly typed objects? :-)

    – Bo Mortensen
    Mar 6 at 15:20













1












1








1


1






I'm having issues mapping a json string returned from sql.



The structure of the object looks like this



 public class OrderSummaryDto

public List<PrescriptionInfo> PrescriptionsInfo get; set;

public class PrescriptionInfo

public string Name get; set;
public AdministrationInfo Administration get; set;

public class AdministrationInfo

public string Instructions get; set;
public string PackageType get; set;





The json string returned from DB



["Name":"testName","Units":3,"Medium":"flower power","Administration":""Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case"","Name":"testName","Units":3,"Medium":"flower power","Administration":""Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case""]


The Sql that generates the json string



(SELECT _co.[Name] 
,_co.[Pharmacy_Instructions] AS [Administration]
FROM [dbo].[Compounds] _co
WHERE _co.[Id] = 1
FOR JSON PATH) AS [PrescriptionsInfo]


Pharmacy_Instructions is already a JSON formated string




Message "Could not cast or convert from System.String to
Models.Order.Summary.OrderSummaryDto+PrescriptionInfo+AdministrationInfo." string











share|improve this question














I'm having issues mapping a json string returned from sql.



The structure of the object looks like this



 public class OrderSummaryDto

public List<PrescriptionInfo> PrescriptionsInfo get; set;

public class PrescriptionInfo

public string Name get; set;
public AdministrationInfo Administration get; set;

public class AdministrationInfo

public string Instructions get; set;
public string PackageType get; set;





The json string returned from DB



["Name":"testName","Units":3,"Medium":"flower power","Administration":""Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case"","Name":"testName","Units":3,"Medium":"flower power","Administration":""Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case""]


The Sql that generates the json string



(SELECT _co.[Name] 
,_co.[Pharmacy_Instructions] AS [Administration]
FROM [dbo].[Compounds] _co
WHERE _co.[Id] = 1
FOR JSON PATH) AS [PrescriptionsInfo]


Pharmacy_Instructions is already a JSON formated string




Message "Could not cast or convert from System.String to
Models.Order.Summary.OrderSummaryDto+PrescriptionInfo+AdministrationInfo." string








c# json serialization deserialization






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 6 at 15:12









MCRMCR

680320




680320







  • 2





    related: stackoverflow.com/questions/40804648/…

    – Meg
    Mar 6 at 15:15






  • 4





    Administration appears to have been double serialized as evident by "{"Instructions"....

    – Nkosi
    Mar 6 at 15:15











  • your json string seems to be an array of PrescriptionsInfo not a OrderSummaryDto

    – tschmit007
    Mar 6 at 15:18











  • Out of curiosity: why not use a (micro) ORM such as Dapper make your quries return strongly typed objects? :-)

    – Bo Mortensen
    Mar 6 at 15:20












  • 2





    related: stackoverflow.com/questions/40804648/…

    – Meg
    Mar 6 at 15:15






  • 4





    Administration appears to have been double serialized as evident by "{"Instructions"....

    – Nkosi
    Mar 6 at 15:15











  • your json string seems to be an array of PrescriptionsInfo not a OrderSummaryDto

    – tschmit007
    Mar 6 at 15:18











  • Out of curiosity: why not use a (micro) ORM such as Dapper make your quries return strongly typed objects? :-)

    – Bo Mortensen
    Mar 6 at 15:20







2




2





related: stackoverflow.com/questions/40804648/…

– Meg
Mar 6 at 15:15





related: stackoverflow.com/questions/40804648/…

– Meg
Mar 6 at 15:15




4




4





Administration appears to have been double serialized as evident by "{"Instructions"....

– Nkosi
Mar 6 at 15:15





Administration appears to have been double serialized as evident by "{"Instructions"....

– Nkosi
Mar 6 at 15:15













your json string seems to be an array of PrescriptionsInfo not a OrderSummaryDto

– tschmit007
Mar 6 at 15:18





your json string seems to be an array of PrescriptionsInfo not a OrderSummaryDto

– tschmit007
Mar 6 at 15:18













Out of curiosity: why not use a (micro) ORM such as Dapper make your quries return strongly typed objects? :-)

– Bo Mortensen
Mar 6 at 15:20





Out of curiosity: why not use a (micro) ORM such as Dapper make your quries return strongly typed objects? :-)

– Bo Mortensen
Mar 6 at 15:20












1 Answer
1






active

oldest

votes


















1














Your JSON string is currently a list of PrescriptionInfo Objects, not a OrderSummary object that contains a list of PrescriptionInfo objects. In addition, some fields (Administration) are being treated as strings, instead of objects. The JSON needs to look like this to work properly:



"PrescriptionsInfo": ["Name":"testName","Units":3,"Medium":"flower power","Administration":"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case","Name":"testName","Units":3,"Medium":"flower power","Administration":"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case"]


Or formatted:



 
"PrescriptionsInfo":[

"Name":"testName",
"Units":3,
"Medium":"flower power",
"Administration":
"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq",
"PackageType":"case"

,

"Name":"testName",
"Units":3,
"Medium":"flower power",
"Administration":
"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq",
"PackageType":"case"


]



In order to do this you will have to use aliases. Check out this MS guide
https://docs.microsoft.com/en-us/sql/relational-databases/json/format-nested-json-output-with-path-mode-sql-server?view=sql-server-2017



I am weak with JSON support in SQL, might ask with those tags, but it seems like it would be something similar to this.



(SELECT _co.[Name], 
JSON_VALUE(_co.[Pharmacy_Instructions], '$.Instructions') AS "Administration.Instructions",
JSON_VALUE(_co.[Pharmacy_Instructions], '$.PackageType') AS "Administration.PackageType"
FROM [dbo].[Compounds] _co
WHERE _co.[Id] = 1
FOR JSON PATH) AS [PrescriptionsInfo]





share|improve this answer

























  • thanks for the answer, I'm already investigating the documentation

    – MCR
    Mar 6 at 15:35











  • I don't have a SQL 2016 server to test this on, but I added what theoretically is what you are needing. You will need to open up the stored JSON string, and then take out the pieces and alias them as the subcomponents that they are. My syntax may be slightly off, as I can't test. I did try to, but my server doesn't support JSON.

    – Tim
    Mar 6 at 15:38










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55026347%2fdeserializing-a-json-string-into-structure-results-in-exception%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your JSON string is currently a list of PrescriptionInfo Objects, not a OrderSummary object that contains a list of PrescriptionInfo objects. In addition, some fields (Administration) are being treated as strings, instead of objects. The JSON needs to look like this to work properly:



"PrescriptionsInfo": ["Name":"testName","Units":3,"Medium":"flower power","Administration":"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case","Name":"testName","Units":3,"Medium":"flower power","Administration":"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case"]


Or formatted:



 
"PrescriptionsInfo":[

"Name":"testName",
"Units":3,
"Medium":"flower power",
"Administration":
"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq",
"PackageType":"case"

,

"Name":"testName",
"Units":3,
"Medium":"flower power",
"Administration":
"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq",
"PackageType":"case"


]



In order to do this you will have to use aliases. Check out this MS guide
https://docs.microsoft.com/en-us/sql/relational-databases/json/format-nested-json-output-with-path-mode-sql-server?view=sql-server-2017



I am weak with JSON support in SQL, might ask with those tags, but it seems like it would be something similar to this.



(SELECT _co.[Name], 
JSON_VALUE(_co.[Pharmacy_Instructions], '$.Instructions') AS "Administration.Instructions",
JSON_VALUE(_co.[Pharmacy_Instructions], '$.PackageType') AS "Administration.PackageType"
FROM [dbo].[Compounds] _co
WHERE _co.[Id] = 1
FOR JSON PATH) AS [PrescriptionsInfo]





share|improve this answer

























  • thanks for the answer, I'm already investigating the documentation

    – MCR
    Mar 6 at 15:35











  • I don't have a SQL 2016 server to test this on, but I added what theoretically is what you are needing. You will need to open up the stored JSON string, and then take out the pieces and alias them as the subcomponents that they are. My syntax may be slightly off, as I can't test. I did try to, but my server doesn't support JSON.

    – Tim
    Mar 6 at 15:38















1














Your JSON string is currently a list of PrescriptionInfo Objects, not a OrderSummary object that contains a list of PrescriptionInfo objects. In addition, some fields (Administration) are being treated as strings, instead of objects. The JSON needs to look like this to work properly:



"PrescriptionsInfo": ["Name":"testName","Units":3,"Medium":"flower power","Administration":"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case","Name":"testName","Units":3,"Medium":"flower power","Administration":"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case"]


Or formatted:



 
"PrescriptionsInfo":[

"Name":"testName",
"Units":3,
"Medium":"flower power",
"Administration":
"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq",
"PackageType":"case"

,

"Name":"testName",
"Units":3,
"Medium":"flower power",
"Administration":
"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq",
"PackageType":"case"


]



In order to do this you will have to use aliases. Check out this MS guide
https://docs.microsoft.com/en-us/sql/relational-databases/json/format-nested-json-output-with-path-mode-sql-server?view=sql-server-2017



I am weak with JSON support in SQL, might ask with those tags, but it seems like it would be something similar to this.



(SELECT _co.[Name], 
JSON_VALUE(_co.[Pharmacy_Instructions], '$.Instructions') AS "Administration.Instructions",
JSON_VALUE(_co.[Pharmacy_Instructions], '$.PackageType') AS "Administration.PackageType"
FROM [dbo].[Compounds] _co
WHERE _co.[Id] = 1
FOR JSON PATH) AS [PrescriptionsInfo]





share|improve this answer

























  • thanks for the answer, I'm already investigating the documentation

    – MCR
    Mar 6 at 15:35











  • I don't have a SQL 2016 server to test this on, but I added what theoretically is what you are needing. You will need to open up the stored JSON string, and then take out the pieces and alias them as the subcomponents that they are. My syntax may be slightly off, as I can't test. I did try to, but my server doesn't support JSON.

    – Tim
    Mar 6 at 15:38













1












1








1







Your JSON string is currently a list of PrescriptionInfo Objects, not a OrderSummary object that contains a list of PrescriptionInfo objects. In addition, some fields (Administration) are being treated as strings, instead of objects. The JSON needs to look like this to work properly:



"PrescriptionsInfo": ["Name":"testName","Units":3,"Medium":"flower power","Administration":"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case","Name":"testName","Units":3,"Medium":"flower power","Administration":"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case"]


Or formatted:



 
"PrescriptionsInfo":[

"Name":"testName",
"Units":3,
"Medium":"flower power",
"Administration":
"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq",
"PackageType":"case"

,

"Name":"testName",
"Units":3,
"Medium":"flower power",
"Administration":
"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq",
"PackageType":"case"


]



In order to do this you will have to use aliases. Check out this MS guide
https://docs.microsoft.com/en-us/sql/relational-databases/json/format-nested-json-output-with-path-mode-sql-server?view=sql-server-2017



I am weak with JSON support in SQL, might ask with those tags, but it seems like it would be something similar to this.



(SELECT _co.[Name], 
JSON_VALUE(_co.[Pharmacy_Instructions], '$.Instructions') AS "Administration.Instructions",
JSON_VALUE(_co.[Pharmacy_Instructions], '$.PackageType') AS "Administration.PackageType"
FROM [dbo].[Compounds] _co
WHERE _co.[Id] = 1
FOR JSON PATH) AS [PrescriptionsInfo]





share|improve this answer















Your JSON string is currently a list of PrescriptionInfo Objects, not a OrderSummary object that contains a list of PrescriptionInfo objects. In addition, some fields (Administration) are being treated as strings, instead of objects. The JSON needs to look like this to work properly:



"PrescriptionsInfo": ["Name":"testName","Units":3,"Medium":"flower power","Administration":"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case","Name":"testName","Units":3,"Medium":"flower power","Administration":"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq","PackageType":"case"]


Or formatted:



 
"PrescriptionsInfo":[

"Name":"testName",
"Units":3,
"Medium":"flower power",
"Administration":
"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq",
"PackageType":"case"

,

"Name":"testName",
"Units":3,
"Medium":"flower power",
"Administration":
"Instructions":"drIVnkLEm0b24OK9ceMeeF2fq",
"PackageType":"case"


]



In order to do this you will have to use aliases. Check out this MS guide
https://docs.microsoft.com/en-us/sql/relational-databases/json/format-nested-json-output-with-path-mode-sql-server?view=sql-server-2017



I am weak with JSON support in SQL, might ask with those tags, but it seems like it would be something similar to this.



(SELECT _co.[Name], 
JSON_VALUE(_co.[Pharmacy_Instructions], '$.Instructions') AS "Administration.Instructions",
JSON_VALUE(_co.[Pharmacy_Instructions], '$.PackageType') AS "Administration.PackageType"
FROM [dbo].[Compounds] _co
WHERE _co.[Id] = 1
FOR JSON PATH) AS [PrescriptionsInfo]






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 6 at 17:01

























answered Mar 6 at 15:24









TimTim

2,1951715




2,1951715












  • thanks for the answer, I'm already investigating the documentation

    – MCR
    Mar 6 at 15:35











  • I don't have a SQL 2016 server to test this on, but I added what theoretically is what you are needing. You will need to open up the stored JSON string, and then take out the pieces and alias them as the subcomponents that they are. My syntax may be slightly off, as I can't test. I did try to, but my server doesn't support JSON.

    – Tim
    Mar 6 at 15:38

















  • thanks for the answer, I'm already investigating the documentation

    – MCR
    Mar 6 at 15:35











  • I don't have a SQL 2016 server to test this on, but I added what theoretically is what you are needing. You will need to open up the stored JSON string, and then take out the pieces and alias them as the subcomponents that they are. My syntax may be slightly off, as I can't test. I did try to, but my server doesn't support JSON.

    – Tim
    Mar 6 at 15:38
















thanks for the answer, I'm already investigating the documentation

– MCR
Mar 6 at 15:35





thanks for the answer, I'm already investigating the documentation

– MCR
Mar 6 at 15:35













I don't have a SQL 2016 server to test this on, but I added what theoretically is what you are needing. You will need to open up the stored JSON string, and then take out the pieces and alias them as the subcomponents that they are. My syntax may be slightly off, as I can't test. I did try to, but my server doesn't support JSON.

– Tim
Mar 6 at 15:38





I don't have a SQL 2016 server to test this on, but I added what theoretically is what you are needing. You will need to open up the stored JSON string, and then take out the pieces and alias them as the subcomponents that they are. My syntax may be slightly off, as I can't test. I did try to, but my server doesn't support JSON.

– Tim
Mar 6 at 15:38



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55026347%2fdeserializing-a-json-string-into-structure-results-in-exception%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко