Creating reduced json from a bigger json in c# [duplicate]

情到浓时终转凉″ 提交于 2019-12-11 05:24:34

问题


I have a json that is coming from the server similar to this (with multiple nested json objects).

{
"employee": [{
        "fullname": {
            "firstname": "abcd",
            "lastname": "defg"
        },
        "project": [{
                "projectname":"abcd_1",
                "datejoined": "2019-06-18T01:29:38.6013262+00:00",
                "projectmanager": "abcdM1",
            }, {
                "projectname":"abcd_2",
                "datejoined": "2018-06-18T01:29:38.6013262+00:00",
                "projectmanager": "abcdM2",
            }, {
                "projectname":"abcd_3",
                "datejoined": "2017-06-18T01:29:38.6013262+00:00",
                "projectmanager": "abcdM3",
            }
        ]
    },{
        "fullname": {
            "firstname": "abcd",
            "lastname": "defg"
        },
        "project": [{
                "projectname":"abcd_1",
                "datejoined": "2019-06-18T01:29:38.6013262+00:00",
                "projectmanager": "abcdM1",
            }, {
                "projectname":"abcd_2",
                "datejoined": "2018-06-18T01:29:38.6013262+00:00",
                "projectmanager": "abcdM2",
            }, {
                "projectname":"abcd_3",
                "datejoined": "2017-06-18T01:29:38.6013262+00:00",
                "projectmanager": "abcdM3",
            }
        ]
      }
    ]

}

The service component will send only the relevant data in a reduced json format to the UX.
I want to extract employee.fullname.firstname and employee.project.projectname.

The output should be

{
"employee": [{
        "fullname": {
            "firstname": "abcd",
        },
        "project": [{
                "projectname":"abcd_1",
            }, {
                "projectname":"abcd_2",
            }, {
                "projectname":"abcd_3",
            }
        ]
    },{
        "fullname": {
            "firstname": "abcd",

        },
        "project": [{
                "projectname":"abcd_1",

            }, {
                "projectname":"abcd_2",

            }, {
                "projectname":"abcd_3",

            }
        ]
      }
    ]

}

I flattened the Json but it gives the tags as employee.0.fullname.firstname and employee.0.project.0.projectname etc

What is the best way to extract with/without flattening?


回答1:


Here's an example that uses a poco with only the properties you want, you can deserialize this in this object, then serialize back to json to get what you want.

void Main()
{
    var myJson = @"
    {
        ""employee"": [{
            ""fullname"": {
                ""firstname"": ""abcd"",
                ""lastname"": ""defg""
            },
            ""project"": [{
                    ""projectname"":""abcd_1"",
                    ""datejoined"": ""2019-06-18T01:29:38.6013262+00:00"",
                    ""projectmanager"": ""abcdM1"",
                }, {
                    ""projectname"":""abcd_2"",
                    ""datejoined"": ""2018-06-18T01:29:38.6013262+00:00"",
                    ""projectmanager"": ""abcdM2"",
                }, {
                    ""projectname"":""abcd_3"",
                    ""datejoined"": ""2017-06-18T01:29:38.6013262+00:00"",
                    ""projectmanager"": ""abcdM3"",
                }
            ]
        },{
            ""fullname"": {
                ""firstname"": ""abcd"",
                ""lastname"": ""defg""      
            },
            ""project"": [{
                    ""projectname"":""abcd_1"",
                    ""datejoined"": ""2019-06-18T01:29:38.6013262+00:00"",
                    ""projectmanager"": ""abcdM1"",
                }, {
                    ""projectname"":""abcd_2"",
                    ""datejoined"": ""2018-06-18T01:29:38.6013262+00:00"",
                    ""projectmanager"": ""abcdM2"",
                }, {
                    ""projectname"":""abcd_3"",
                    ""datejoined"": ""2017-06-18T01:29:38.6013262+00:00"",
                    ""projectmanager"": ""abcdM3"",
                }
            ]
          }
        ]
    }";


    var myObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Employee>(myJson);
    var myTrimmedJson = Newtonsoft.Json.JsonConvert.SerializeObject(myObject);
    Console.WriteLine(myTrimmedJson);
}

public class Fullname
{
    public String firstname { get; set; }
    //public String lastname { get; set; }
}

public class Project
{
    public String projectname { get; set; }
    //public String datejoined { get; set; }
    //public String projectmanager { get; set; }
}

public class Person
{
    public Fullname fullname { get; set; }
    public List<Project> project { get; set; }
}

public class Employee
{
    public List<Person> employee { get; set; }
}



回答2:


You can create small small interfaces, first convert json in to class object and then typecast into the specific Interface, and then again serialize it using newtonSoft, it will give small JSON



来源:https://stackoverflow.com/questions/56764017/creating-reduced-json-from-a-bigger-json-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!