Home > Software design >  How to serialize object without curly braces in .Net Core
How to serialize object without curly braces in .Net Core

Time:02-03

I am using NewtonJson for serializing object. I want to serlize a object which has two properties one is normal string and the second property is dictionary of some items.

I am expecting a result something like this:

"Company": {
            "Id": "1393",
            "emp1": {
                "email": "[email protected]",
                "firstName": "test1",
                "lastName": "test1",
                "title": "Mr"
            },
            "emp2": {
                "email": "[email protected]",
                "firstName": "test2",
                "lastName": "test2",
                "title": "Ms"
            }
        }

but I am getting output like below:

"Company": {
            "Id": "1393",
            "employees": {
                "emp1": {
                    "email": "[email protected]",
                    "firstName": "test1",
                    "lastName": "test1",
                    "title": "Mr"
                 },
                 "emp2": {
                    "email": "[email protected]",
                    "firstName": "test2",
                    "lastName": "test2",
                    "title": "Ms"
              }
            }
        }

Here is my Code:

public string GetCompany(Dictionary<string, Employee> employees)
        {
            var company = JsonConvert.SerializeObject(new
            {
                Id = "1393",
                employees
            });

            return company;
        }

CodePudding user response:

Here is a whole working demo you could follow:

public string GetCompany(Dictionary<string, Employee> employees)
{
    var model = new Dictionary<string, object>();
    model["Id"] = "1393";
    foreach (var employee in employees)
    {
        model[employee.Key] = employee.Value;
    }
        
    var company = JsonConvert.SerializeObject(new { Company =model});

    return company;
}

CodePudding user response:

By creating the anonymous object, your adding another property named employees.

Why not just append Id to the dictionary?

e.g.

var employeesCopy = employees
    .ToDictionary(kvp => kvp.Key, kvp => (object)kvp.Value);

employeesCopy["Id"] = "1393";

var company = JsonConvert.SerializeObject(employeesCopy);

This makes a copy of employees and adds a new key "Id" before serialising.

CodePudding user response:

Create a Poco (Plain Old C# Object). Make sure to add [JsonExtensionData] attribute.

public class Poco
{
    public string Id { get; set; } = string.Empty;

    [JsonExtensionData]
    public Dictionary<string, object> Employees { get; set; } = new();
}

And then serialize it

var employees = new Dictionary<string, object>()
{
    { Path.GetRandomFileName(), new Employee() },
    { Path.GetRandomFileName(), new Employee() }
};

var company = JsonConvert.SerializeObject(new Poco { Id = "1393", Employees = employees });

CodePudding user response:

you can try this

public string GetCompany(Dictionary<string, Employee> employees)
{
    var company = new Dictionary<string, object> { { "Id", "1393" } };
    employees.ToList().ForEach(e => company.Add(e.Key, e.Value));
    return JsonConvert.SerializeObject(company, Newtonsoft.Json.Formatting.Indented);
}
  • Related