0

I have a dictionary with this format:

var dic = new Dictionary<string, string>(){
{"a.b.c", "value1"},
{"a.b.d", "value2"},
{"a.c.e", "value3"},
{"a.c.f", "value4"}
};

I want to achieve this json file format in C#.

{
   "a":{
      "b":{
         "c":"value1",
         "d":"value2"
      },
      "c":{
         "e":"value3",
         "f":"value4"
      }
   }
}

I used JsonConvert.SerializeObject(dic) but the result is in this format:

{
   "a.b.c":"value1",
   "a.b.d":"value2",
   "a.c.e":"value3",
   "a.c.f":"value4"
}

I couldn't find a way to convert this format to the tree-based JSON format. Do you know any way that I can get it?

ali ak
  • 31
  • 3
  • 3
    All that has been posted is a program description, but that doesn't tell us what _problem_ you're having. What have you tried, and what troubles did you encounter? Please [edit] your post to include a [valid question](//stackoverflow.com/help/how-to-ask) that we can answer. Reminder: make sure you know what is [on-topic](//stackoverflow.com/help/on-topic); asking us to write the program for you, suggestions, and external links are off-topic. – gunr2171 Jul 23 '21 at 12:31
  • 2
    The JSON serialiser doesn't know that `"a.b.c"` describes a hierarchy. You're going to have to split those strings yourself, and process them, with the goal to create another object which reflects the hierarchy, and then encode it as JSON. Remember that JSON itself isn't generally something you manipulate directly, it's just a serialisation format for storing or transmitting data. So this isn't really a JSON problem, it's a C# coding problem to create a suitable data structure which you can then serialise. – ADyson Jul 23 '21 at 12:53
  • Hi @aliak I've modified the result of `JsonConvert.SerializeObject(dic)`, because what you have shared us was not a valid json. This is not valid `"a.b.d"="value2"`. I assume it was just a copy-paste issue. If it was intentional then please edit your question. – Peter Csala Jul 23 '21 at 13:10
  • 1
    By the way what's type of the `dic`? `Dictionary`? – Peter Csala Jul 23 '21 at 13:11
  • Your "dot separated path" keys are in [tag:jsonpath] format. Creating a JSON hierarchy from a collection of paths and values is not built into Json.NET, you will have to write it yourself by parsing the path. But check to see whether [this answer](https://stackoverflow.com/a/40858665) by [Danny Su](https://stackoverflow.com/users/1311686/danny-su) to [How to unflatten flattened json in C#](https://stackoverflow.com/q/40541842) does what you want, it has code for recursively creating a `JObject` from a dictionary of paths and values. – dbc Jul 23 '21 at 17:16
  • [How to add a new JProperty to a JSON based on path?](https://stackoverflow.com/q/56427214/3744182) might also work for you, though the answer there does not support arrays. – dbc Jul 23 '21 at 17:17
  • [How to unflatten flattened json in C#](https://stackoverflow.com/q/40541842/3744182) works, see https://dotnetfiddle.net/C9Q2t3. Closing as a duplicate. – dbc Jul 23 '21 at 17:21

0 Answers0