1

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?

narasimman
  • 421
  • 3
  • 14
  • 1
    Would `JsonExtensions.RemoveAllExcept(this JObject obj, IEnumerable paths)` from [this answer](https://stackoverflow.com/a/30333562/3744182) to [How to perform partial object serialization providing “paths” using Newtonsoft JSON.NET](https://stackoverflow.com/q/30304128/3744182) meet your needs? – dbc Jun 26 '19 at 01:49
  • Thanks for the reply. I think it will work. Will try and let you know. Once again thanks. I have been struggling for the last couple of days. – narasimman Jun 26 '19 at 02:07
  • The above solution works perfectly for my situation. Only issue is that if there are duplicates, say projectname in the above example, only the first instance is returned. I require all other instances as well. – narasimman Jun 26 '19 at 16:53
  • You should be able to use [JSONPath wildcards](https://stackoverflow.com/a/43458821), e.g. `employee[*].project[*].projectname`. – dbc Jun 26 '19 at 17:01
  • I tried it. The result is { "employee": [ { "project": [ { "projectname": "abcd_1" }, { "projectname": "abcd_2" }, { "projectname": "abcd_3" } ] } ] } The second set of project names is not coming up. – narasimman Jun 26 '19 at 17:11
  • Looks like there's a bug in that function. Will fix... – dbc Jun 26 '19 at 17:45
  • Try it now, see https://dotnetfiddle.net/10Un8F – dbc Jun 26 '19 at 18:21
  • Thanks dbc. It works like a charm. – narasimman Jun 26 '19 at 18:45
  • OK, so should we mark this as a duplicate, or do you think I should add an answer referring back the old question? – dbc Jun 26 '19 at 18:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195591/discussion-between-narasimman-and-dbc). – narasimman Jun 26 '19 at 18:54

2 Answers2

0

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; }
}
Carlo Bos
  • 3,105
  • 2
  • 16
  • 29
0

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

Shobhit Walia
  • 496
  • 4
  • 19