0

I am receiving a json as follows:

{
  "soce":{
  "message":{"details":"No Details available now.","code":"123"},
  "number":"500"
  }
}

I am using Newtonsoft JsonConvert. I do not want to serialize the value in message as json but as a string.

I tried to update the json to include the value of message in quotes but that did not work.

{
  "soce":{
  "message":"{"details":"No Details available now.","code":"123"}",
  "number":"500"
  }
}

Is there a way to indicate the deserializer not to deserialize a part of json?

user2347528
  • 580
  • 1
  • 8
  • 22
  • Your last example won't work because it is not legal json. You need to escape double quotes in strings for them to not be handled as end-of-string. – Lasse V. Karlsen Apr 29 '20 at 21:23
  • 1
    Why do you want it as a string? Is it because you don't want to incur the overhead of deserializing this part, or do you just want to have the raw json at that level? – Lasse V. Karlsen Apr 29 '20 at 21:24
  • 1
    If you just want to get *a* json representation of whatever is in `message`, you can declare a property of type `JToken` for that property and deserialize into it, then use `.ToString()` on it to get json. It will not necessarily match whitespace in the original json input, however. – Lasse V. Karlsen Apr 29 '20 at 21:30
  • Yes Lasse, I need the raw json content as-is without deserializing only for the content in message – user2347528 Apr 30 '20 at 18:47
  • Do these answer your question? [Efficiently get full json string in JsonConverter.ReadJson()](https://stackoverflow.com/q/56944160/3744182) and [https://stackoverflow.com/q/56944160/3744182](https://stackoverflow.com/q/40529125/3744182). – dbc Apr 30 '20 at 20:17

1 Answers1

0

As Lasse karlsen suggested, I declared the property of type JToken and I use ToString() to get the value as json string.

public JToken message { get; set; }

user2347528
  • 580
  • 1
  • 8
  • 22