1

I have a json list of Different Type of objects (Schemaless). however, every row contains the Type of the structure. I need to group it as per their type. I need to group this Json. Input is below

 var result =   [{ "firstName":"John", "lastName":"Doe", "Type":"Person" },
    { "fruit":"apple", "Type":"Diet" },
    { "length":100, "width":60, "height":30,, "Type":"Measure" },
    { "firstName":"Shivang", "lastName":"Mittal", "Type":"Person" }]

Expected Output:

    var persons=   [{ "firstName":"John", "lastName":"Doe", "Type":"Person" }, 
                    { "firstName":"Shivang", "lastName":"Mittal", "Type":"Person" }];

    var Diets = [{ "fruit":"apple", "Type":"Diet" }];
    var Measures = [{ "length":100, "width":60, "height":30,, "Type":"" }];

How can i group it based on their Type in c#?

9
  • What do you want as result? Json? C# objects? Commented Apr 17, 2015 at 11:17
  • @greenhoorn : Json Iteself, in diff groups. So basically i need to segregate it by type Commented Apr 17, 2015 at 11:21
  • @OndrejSvejdar: Sorry, my bad. corrected Commented Apr 17, 2015 at 11:31
  • 1
    You could make the question clearer... anyhow have a read of stackoverflow.com/questions/27685829/… Commented Apr 17, 2015 at 11:40
  • Then have a read of using newtonsoft.com/json to achieve grouping stackoverflow.com/questions/1698175/… Commented Apr 17, 2015 at 11:42

2 Answers 2

2

Without using strongly-typed objects/changing schema:

  var jarray = JArray.Parse(@"[{ ""firstName"":""John"", ""lastName"":""Doe"", ""Type"":""Person"" },
{ ""fruit"":""apple"", ""Type"":""Diet"" },
{ ""length"":100, ""width"":60, ""height"":30, ""Type"":""Measure"" },
{ ""firstName"":""Shivang"", ""lastName"":""Mittal"", ""Type"":""Person"" }]");

  var query = jarray
    .GroupBy(g => g["Type"])
    .Select(g => string.Format("var {0}={1};{2}", g.Key, new JArray(g.ToList()), Environment.NewLine));
  string result = string.Join(string.Empty, query);
Sign up to request clarification or add additional context in comments.

Comments

1

better for you will be change your json format:

{
   "Customer": [{"firstName":"John", "lastName":"Doe", "Type":"Person" }],
   "Fruit": [{"fruit":"apple", "Type":"Diet" }],
   "Length": [{"length":100, "width":60, "height":30, "Type":"Measure" }]
}

and then it will be easy

public class JsonType()
{
    public CustomerType Customer {get; set;}
    public FruitType Fruit {get; set;}
    public LengthType Length {get; set;}
}

public class CustomerType()
{
    public string firstName {get; set;}
    public string lastName {get; set;}
    public string Type {get; set;}
}

public class FruitType()
{
    public string fruit {get; set;}
    public string Type {get; set;}
}

etc...

var result = JsonConvert.DeserializeObject<JsonType>(yourJson);

1 Comment

nice suggestion. But for that i need to change the end point ( service returning the json ). It will be a time taking work around to change the service

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.