Skip to main content
2 of 4
deleted 1 character in body

Created structured/nested JSON from unstructured JSON in C# - Optimize

I have below unstructured but valid JSON which need to be converted to structured format using any C# library or newtonsoft-

 {
    "root_id": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "root_tittel": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },          
    "root_mottaker_adresse1": {
        "Path": "InsertDocuments",
        "MainContract": "CreateDocumentParameter"
    },
    "root_mottaker_adresse2": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "root_web_id_guid": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    }
}

want to make it structured as below -

{
    "id": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },
    "tittel": {
        "Path": "InsertCases",
        "MainContract": "CreateCaseParameter"
    },              
    "mottaker": {
        "adresse1": {
            "Path": "InsertDocuments",
            "MainContract": "CreateDocumentParameter"
        },
        "adresse2": {
            "Path": "InsertCases",
            "MainContract": "CreateCaseParameter"
        }
    },
    "web": {
        "id": {
            "guid": {
                "Path": "InsertCases",
                "MainContract": "CreateCaseParameter"
            }
        }
    }
}

if you see the difference the hierarchy is splitted with _(underscore). I want to make it in a more nested way.

i.e.

  1. root_element -> element
  2. root_element1_element2 -> element1 is parent and element2 is child.

Code

JObject obj = JObject.Parse(jsonString);
            JObject finalObj = new JObject();
            foreach (var item in obj)
            {
                var keys = item.Key.Replace("root_", "").Split("_").Reverse();
                bool nestedKeyProcessed = false;
                JObject tempObj = new JObject();
                foreach (string key in keys)
                {
                    if (keys.Count() > 1 && !nestedKeyProcessed)
                    {
                        tempObj = CreateJObject(key, item.Value);
                        nestedKeyProcessed = true;
                    }
                    else
                    {
                        if (keys.Count() == 1)
                            finalObj.Add(new JProperty(key, item.Value));
                        else
                            tempObj = CreateJObjectUsingJProperty(key, tempObj);
                    }
                }
                if (keys.Count() > 1)
                    finalObj.Merge(tempObj, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Union });
            }
            string json = JsonConvert.SerializeObject(finalObj);
            JObject CreateJObject(string key, JToken? data)
            {
                JObject obj = new JObject();
                obj.Add(key, data);
                return obj;
            }
            JObject CreateJObjectUsingJProperty(string key, object? data)
            {
                JObject obj = new JObject(new JProperty(key, data));
                return obj;
            }

Please review and let me know if we can do in any optimized way..

Thanks in advance!