1

I am trying to deserialize JSON into an object so I can add it to elastic search. JSON can be of many different object types in the project so I would like the function to be dynamic.

First I am serializing the Data that I get from EF Core context

var serializedObject = JsonConvert.SerializeObject(document, Formatting.None,
                       new JsonSerializerSettings()
                       {
                           ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                       });

Next I would like to deserialize to an object. For example if I have

public class EValues 
{
    public dynamic values { get; set; }
}

var test =  JsonConvert.DeserializeObject<EValues>(serializedObject.ToString());

I would like the JSON to be deserialized to the below:

{
   "values":{
      "StudentId":"60712555-ff1d-4a3e-8c81-08d9c2fc4423",
      "Student":{
         "Name":"string",
         "Country":"string",
         "Street":"string"
      }
   }
}

The serializedObject JSON I am actually trying to deserialize:

{
   "StudentId":"60712555-ff1d-4a3e-8c81-08d9c2fc4423",
   "Student":{
      "Name":"string",
      "Country":"string",
      "Street":"string"
   }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
Learn AspNet
  • 1,192
  • 3
  • 34
  • 74
  • So, what is your question? It seems like `JsonConvert.DeserializeObject(serializedObject.ToString())` should work; have you tried it? Is there a problem? – dbc Dec 20 '21 at 04:36
  • The values is always null in this case when I do deserialize @dbc – Learn AspNet Dec 20 '21 at 13:33
  • And where is the problem? Is it that the values in the string `serializedObject` are null, or that `serializedObject` has the correct data but the values in `test.values` are null? I tried to deserialize the JSON shown in your question to `EValues` and I cannot reproduce your problem, see https://dotnetfiddle.net/KGmTs1. So I think we will need to see a [mcve] to help you. – dbc Dec 20 '21 at 14:07
  • @dbc I updated the question with the JSON it currently has. I would like the values to be there in front of it – Learn AspNet Dec 20 '21 at 15:12
  • 1
    Why not just do `var test = new EValues { values = JsonConvert.DeserializeObject(serializedObject) };`? Your input JSON doesn't have the `{ "values" : {} }` nesting so it's reasonable that it cannot be deserialized to `EValues`, which does. – dbc Dec 20 '21 at 15:31
  • @dbc This works. You can add it as an answer and I can accept it. Also, when I deserialize the object, it adds an extra bracket and then the json is not proper. Is there a way to remove that extra bracket? – Learn AspNet Dec 20 '21 at 15:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/240288/discussion-between-learn-aspnet-and-dbc). – Learn AspNet Dec 20 '21 at 15:51
  • There's no extra bracket. Visual Studio is "helpfully" adding that when the value is visualized. To see what the value really looks like, in the Immediate Window do `Console.WriteLine(test.values)`. – dbc Dec 20 '21 at 15:55
  • @dbc Console.WriteLine does not show it but when I try to add this values to Elasticsearch the values document is empty when I see in Elastic Search and Kibana – Learn AspNet Dec 20 '21 at 17:22
  • You should ask another question as we would need an [mcve] for that. Possibly the code to add the values to Elastic Search is using a different serializer such as System.Text.Json with no direct support for `JToken` (the actual type Json.NET uses for dynamic data). – dbc Dec 20 '21 at 18:03
  • @dbc Here it is: https://stackoverflow.com/questions/70426312/elastic-search-adding-empty-values-for-all-the-documents-how-can-i-add-complete – Learn AspNet Dec 20 '21 at 18:18

1 Answers1

2

You can just do:

var test = new EValues { 
    values = JsonConvert.DeserializeObject<dynamic>(serializedObject) 
};

The JSON that would correspond to EValues would have an extra level of nesting { "values" : {} } not present in your serializedObject JSON.

dbc
  • 104,963
  • 20
  • 228
  • 340