Click here to Skip to main content
14,699,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using a Dictionary to create Json file. The data in the Dictionary would look similar to this:
Dictionary<string, string> inputValues = new Dictionary<string, string>();
inputValues.Add("Employee.Person.PersonalInfo.HRCode", "HR1234");
inputValues.Add("Employee.Person.PersonalInfo.FirstName", "ABC");
inputValues.Add("Employee.Person.PersonalInfo.LastName", "XYZ");
inputValues.Add("Employee.Person.Salary[0].Amount", "76850.00");
inputValues.Add("Employee.Person.Salary[0].Currency", "USD");
inputValues.Add("Employee.Person.Salary[1].Amount", "87954.00");
inputValues.Add("Employee.Person.Salary[1].Currency", "INR");
inputValues.Add("Employee.Address.[0].Home.[0].Address1", "Address0Home0 Address1");
inputValues.Add("Employee.Address.[0].Home.[0].Address2", "Address0.Home0 Address2");
inputValues.Add("Employee.Address.[1].Home.[0].Address1", "Address1.Home0 Address1");
inputValues.Add("Employee.Address.[1].Home.[0].Address2", "Address1.Home0 Address2");


Values in the Dictionary will be populate dynamically so we cannot predict the values. It could be any name for the key and any depth indicated by a dot something like: "key1.key2.keyN". Now I want to serialize every key and value I get from the DIctionary into Json. If the key is an index value like: [0], [1] etc, rest all the keys after this should be created as an array under its parent. The expected output with the test data from above will look like this:

{
    "Employee": {
        "Person": {
            "PersonalInfo": {
                "HRCode": "HR1234",
                "FirstName": "ABC",
                "LastName": "XYZ"
            },
            "Salary": [
                {
                    "Amount": "76850.00",
                    "Currency": "USD"
                },
                {
                    "Amount": "87954.00",
                    "Currency": "INR"
                }
            ],
            "Address": [
                {
                    "Home": [
                        {
                            "Address1": "Address0Home0 Address1",
                            "Address2": "Address0.Home0 Address2"
                        }
                    ]
                },
                {
                    "Home": [
                        {
                            "Address1": "Address1.Home0 Address1",
                            "Address2": "Address1.Home0 Address2"
                        }
                    ]
                }
            ]
        }
    }
}


What I have tried:

I'm using JSON.NET but I barely scratched the surface and don't know if there is a Method for this. I tried something as below to add elements to separate Dictionaries created in a hierarchical structure. It is working as expected for nodes which doesn't have any index.

Dictionary<string, string> inputValues = new Dictionary<string, string>();
inputValues.Add("Employee.Person.PersonalInfo.HRCode", "HR1234");
inputValues.Add("Employee.Person.PersonalInfo.FirstName", "Sumith");
inputValues.Add("Employee.Person.PersonalInfo.LastName", "Joy");
inputValues.Add("Employee.Person.Salary.[0].Amount", "76850.00");
inputValues.Add("Employee.Person.Salary.[0].Currency", "USD");
inputValues.Add("Employee.Person.Salary.[1].Amount", "87954.00");
inputValues.Add("Employee.Person.Salary.[1].Currency", "INR");
inputValues.Add("Employee.Address.[0].Home.[0].Address1", "Home1.Address1");
inputValues.Add("Employee.Address.[0].Home.[0].Address2", "Home1.Address2");
inputValues.Add("Employee.Address.[0].Home.[1].Address1", "Home2.Address1");
inputValues.Add("Employee.Address.[0].Home.[1].Address2", "Home2.Address2");
inputValues.Add("Employee.Address.[0].Home.[1].Home1.[0].Address1", "Home2.Address1");
inputValues.Add("Employee.Address.[0].Home.[1].Home1.[1].Address2", "Home2.Address2");
inputValues.Add("Employee.Person.BankDetails.Primary.AccountNo", "AC5544");

var res = new Dictionary<string, Object>();

foreach (var pair in inputValues)
{
    var key = pair.Key;
    var parts = key.Split('.');
    var currentObj = res;
    for (int i = 0; i < parts.Length - 1; i++)
    {
        var property = parts[i];
        if (!currentObj.Keys.Contains(property))
            currentObj[property] = new Dictionary<string, Object>();
        currentObj = currentObj[property] as Dictionary<string, Object>;
    }

    currentObj[parts[parts.Length - 1]] = pair.Value;
}

var json = JsonConvert.SerializeObject(res, Formatting.Indented);


I am not sure whether my existing Dictionary approach will work for array nodes.

I hope this doesn't seem like an inappropriate question but I really need help with this. I appreciate any tips in the right direction. Thanks in advance.
Posted
Updated 29-Jul-20 19:47pm
v5

1 solution

I think you are creating a nightmare parsing scenario with this strategy.

Why not use your strongly typed classes with the WCF JSON DataContractJsonSerializer: [^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900