1

Method with variable that accept variety of JSON structure to be query over linq...

Currently my result is heading the right direction returning the select result over the linq query but it's hard coded.

I have fews sets of data structure,

example belows:-

var SET_A=[
    {
        "Axel": "C019",
        "Growth": 4.795,
        "Status": "Poor"
    },
    {
        "Axel": "C019",
        "Growth": 4.083333,
        "Status": "Fair"
    },
    {
        "Axel": "C019",
        "Growth": 8.031212,
        "Status": "V.Poor"
    },
    {
        "Axel": "C019",
        "Growth": 10.6275,
        "Status": "V.Poor"
    },
    {
        "Axel": "C019",
        "Growth": 3.876363,
        "Status": "Fair"
    },
    {
        "Axel": "C019",
        "Growth": 7.735714,
        "Status": "V.Poor"
    },
    {
        "Axel": "C020",
        "Growth": 3.196477,
        "Status": "Good"
    },
    {
        "Axel": "C020",
        "Growth": 3.2,
        "Status": "Good"
    },
    {
        "Axel": "C020",
        "Growth": 4.51125,
        "Status": "Fair"
    },
    {
        "Axel": "C020",
        "Growth": 4.125,
        "Status": "Fair"
    }
];

another sample data.

var SET_B = [
    {
        "Class": "H",
        "Points": 252.17,
        "TotalAchieve": 252.17,
        "LastRecord": 2012
    },
    {
        "Class": "A",
        "Points": 36.44,
        "TotalAchieve": 36.44,
        "LastRecord": 2012
    },
    {
        "Class": "B",
        "Points": 442.07,
        "TotalAchieve": 442.07,
        "LastRecord": 2012
    },
    {
        "Class": "C",
        "Points": 852.32,
        "TotalAchieve": 852.32,
        "LastRecord": 2012
    },
    {
        "Class": "D",
        "Points": 903.96,
        "TotalAchieve": 1323.83,
        "LastRecord": 2012
    },
    {
        "Class": "E",
        "Points": 0,
        "TotalAchieve": 0,
        "LastRecord": 2011
    },
    {
        "Class": "J",
        "Points": 0,
        "TotalAchieve": 0,
        "LastRecord": 2011
    },
    {
        "Class": "M",
        "Points": 0,
        "TotalAchieve": 0,
        "LastRecord": 2011
    },
    {
        "Class": "T",
        "Points": 0,
        "TotalAchieve": 0,
        "LastRecord": 2011
    },
    {
        "Class": "T",
        "Points": 2486.96,
        "TotalAchieve": 2906.83,
        "LastRecord": 2012
    }
]

Possiblity of few more others variant of data in valid JSON structure

my execution ..

ExtractSeries(SET_A, "Growth","Status","Axel");

or

ExtractSeries(SET_B, "Points","Class","LastRecord");

OR

ExtractSeries(SET_B, "TotalAchieve","Class","LastRecord");

I am stuck at this where i am trying

  1. to make use of seedA and seedB to be replace at the Select.
  2. to make use of the group by in the linq.

    public List<string> ExtractSeries(string JSONDs, string seedA,string seedB,string groupby)
    {  
        var jss = new JavaScriptSerializer();
        var table = jss.Deserialize<dynamic>JSONDs, 
        dynamic data = System.Web.Helpers.Json.Decode(jss.Serialize(table));
    
        var result = from x in (IEnumerable<dynamic>)data 
                    select new {
                        *x.Growth*,
                        *x.Status*
                     };
    

    }

I am trying to achieve this "kind" of structure

[{
    "Name": "C019",
    "Data": [
        {
            "Growth": 4.796,
            "Status": "Poor"
        },
        {
            "Growth": 4.083333,
            "Status": "Fair"
        },
        {
            "Growth": 8.031212,
            "Status": "V.Poor"
        },
        {
            "Growth": 10.6275,
            "Status": "V.Poor"
        },
        {
            "Growth": 3.876363,
            "Status": "Fair"
        },
        {
            "Growth": 7.735714,
            "Status": "V.Poor"
        },
        {
            "Growth": 3.196477,
            "Status": "Good"
        },
        {
            "Growth": 3.2,
            "Status": "Good"
        },
        {
            "Growth": 4.51125,
            "Status": "Fair"
        },
        {
            "Growth": 4.125,
            "Status": "Fair"
        }
    ]
},....more data with same structure above i.e "Name":"C020"
]

or

[
    {
        "Name": "2012",
        "Data": [
            {
                "Class": "H",
                "Points": 252.17
            },
            {
                "Class": "A",
                "Points": 36.44
            },
            {
                "Class": "B",
                "Points": 442.07
            },
            {
                "Class": "C",
                "Points": 852.32
            },
            {
                "Class": "D",
                "Points": 903.96
            }
        ]
    },
    {
        "Name": "2011",
        "Data": [
            {
                "Class": "E",
                "Points": 0
            },
            {
                "Class": "J",
                "Points": 0
            },
            {
                "Class": "M",
                "Points": 0
            },
            {
                "Class": "T",
                "Points": 0
            }
        ]
    }
]
6
  • so do you want return List<string> or List<"SomethingObjectFromJSON">? Commented Dec 18, 2013 at 14:36
  • List<string> a = new List<string>(); a.add("{[{"Name": "2012", "Data": [...."); public class someObjFrJSON{ public string name{get;set;} public List<string> data{get;set;} } it be great to have it in List<class> but how do i make a dynamic class when there is variant type of data ... the data part could contain.. string or double or int.. Commented Dec 18, 2013 at 14:49
  • Your question is a little hard to understand. I tried to write a converter, but it's hard without having the data to test on. I think you should try deserializing the data to a List<Dictionary<string,string>> object, and then investigate the group clause to produce the output in the format you need. If you can provide C# code containing a string representation of your data, I can help more. Commented Dec 18, 2013 at 14:51
  • @Oliver Sorry, I try my bests. The mock data to test is the var SET_A and var SET_B.. Commented Dec 18, 2013 at 15:10
  • @Oliver here's another sample string SET_C="{\"Axel\":\"C019\",\"Growth\":4.795,\"Status\":\"Poor\"},{\"Axel\":\"C019\",\"Growth\":7.735714,\"Status\":\"V.Poor\"},{\"Axel\":\"C020\",\"Growth\":3.196477,\"Status\":\"Good\"},{\"Axel\":\"C020\",\"Growth\":3.2,\"Status\":\"Good\"},{\"Axel\":\"C020\",\"Growth\":4.51125,\"Status\":\"Fair\"},{\"Axel\":\"C020\",\"Growth\":4.125,\"Status\":\"Fair\"}"; List<string> xList = new List<string>(); xList = ExtractSeries(SET_C, "Growth","Status","Axel"); Commented Dec 18, 2013 at 15:11

1 Answer 1

2

you can use something like this

private static List<SomeClass> ExtractSeries(string JSONDs, string seedA,string seedB,string groupby)
{
    var jss = new JavaScriptSerializer();
    return (from item in jss.Deserialize<List<Dictionary<string, object>>>(JSONDs)
            select new { val = new Dictionary<string, object>(){{ seedA, item[seedA]}, {seedB, item[seedB] }}, groupKey = item[groupby] } into sampleObj
            group sampleObj by sampleObj.groupKey into g
            select new SomeClass{ Name = g.Key, Data = g.Select(i=>i.val).ToList() })
           .ToList();
}

if you serialize this with new JavaScriptSerializer().Serialize(ExtractSeries(SET_B, "Points","Class","LastRecord")) as result you get string as expected

where SomeClasslike this

public class SomeClass{
    public string Name;
    public List<Dictionary<string,object>> Data;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is it. Efficient.. now i can reuse this code again and again.. Thank You! Happy Holiday!

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.