0

I am trying to convert a JSON array to an Object. This is a hacked way but good enough for my purpose.

Basically I am writing a method to get this

var data = [{
    "MonthYearShortName": "Sep-13",
        "TotalCustomers": 1905.0,
        "Aquisition": 317.0,
        "Attrition": 9.0
}, {
    "MonthYearShortName": "FY-14",
        "TotalCustomers": 2158.0,
        "Aquisition": 401.0,
        "Attrition": 15.0909090909091
}]

into something like this

data = [{
    key: 'Attrition',
    color: '#d62728',
    values: [{
        "label": "Sep-13",
        "value": 9
    }, {
        "label": "FY-14",
            "value": 15.0909090909091
    }]
},

{
    key: 'Total Customer',
    color: '#1f77b4',
    values: [{
        "label": "Sep-13",
        "value": 1905
    }, {
        "label": "FY-14",
        "value": 2158
    }]
},

{
    key: 'Aquisition',
    color: '#1f7774',
    values: [{
         "label": "Sep-13",
         "value": 317
    }, {
            "label": "FY-14",
            "value": 401
    }]
}

];

The colors will be static for now. I will be handling it later.

Now getting to my hacked way of getting this (this is crude I know)

I tried something like this to just get the attrition out

 var data = @"[{""MonthYearShortName"": ""Sep-13"",""TotalCustomers"": 1905.0,""Aquisition"": 317.0,""Attrition"": 9.0}, {""MonthYearShortName"": ""FY-14"",""TotalCustomers"": 2158.0,""Aquisition"": 401.0,""Attrition"": 15.0909090909091}]";
                JArray a = JArray.Parse(data);

                var label1 = a[0]["MonthYearShortName"].ToString();
                var label2 = a[1]["MonthYearShortName"].ToString();
                var totalCustomer1 = a[0]["TotalCustomers"].ToString();
                var totalCustomer2 = a[1]["TotalCustomers"].ToString();
                var aquisition1 = a[0]["Aquisition"].ToString();
                var aquisition2 = a[1]["Aquisition"].ToString();
                var attrition1 = a[0]["Attrition"].ToString();
                var attrition2 = a[1]["Attrition"].ToString();
JObject rss1 =
                new JObject(
                    new JProperty("channel",
                        new JObject(
                            new JProperty("Key", "Attrition"),
                            new JProperty("color", "#d62728"),
                            new JProperty("values",
                                new JArray(
                                    from p in a
                                    select new JObject(
                                        new JProperty("label", a[0]["MonthYearShortName"].ToString()),
                                        new JProperty("value", attrition1),
                                        new JProperty("label", a[1]["MonthYearShortName"].ToString()),
                                        new JProperty("value", attrition2)))))));

When I tried this I get a

Can not add property label to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.

Now if someone can suggest a cleaner way (as I cant think of any right now), I would be greatful or if my code could be corrected, that would be helpful.

Thanks

1 Answer 1

1

Can you try doing it like this?

[Test]
public void Test()
{
    var data = @"[
            {'MonthYearShortName': 'Sep-13','TotalCustomers': 1905.0,'Aquisition': 317.0,'Attrition': 9.0},
            {'MonthYearShortName': 'FY-14','TotalCustomers': 2158.0,'Aquisition': 401.0,'Attrition': 15.0909090909091}
            ]";
    dynamic jarr = JArray.Parse(data);

    var attritions = new List<ExpandoObject>();
    var aquisitions = new List<ExpandoObject>();
    var totalCustomers = new List<ExpandoObject>();

    foreach (dynamic o in jarr)
    {
        dynamic attr = new ExpandoObject();
        attr.label = o.MonthYearShortName;
        attr.value = o.Attrition;

        attritions.Add(attr);

        dynamic acq = new ExpandoObject();
        acq.label = o.MonthYearShortName;
        acq.value = o.Aquisition;
        aquisitions.Add(acq);

        dynamic cust = new ExpandoObject();
        cust.label = o.MonthYearShortName;
        cust.value = o.TotalCustomers;
        totalCustomers.Add(acq);

    }


    dynamic attrition = new ExpandoObject();
    dynamic aquisition = new ExpandoObject();
    dynamic totalCustomer = new ExpandoObject();

    attrition.Key = "Attrition";
    attrition.Color = "#d62728";
    attrition.Values = attritions;

    aquisition.Key = "Acquisition";
    aquisition.Color = "#1f7774";
    aquisition.Values = aquisitions;

    totalCustomer.Key = "Total Customer";
    totalCustomer.Color = "#1f77b4";
    totalCustomer.Values = totalCustomers;

    var result = new[] { attrition,totalCustomer, aquisition };
    var resultString = JsonConvert.SerializeObject(result, Formatting.Indented);
    Console.Write(resultString);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I get an error 'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'MonthYearShortName' at ` attr.label = o.MonthYearShortName;`
did you use exactly the same types, i.e dynamic, also I've tested it with newtonsoft 6.0.1
I am trying to use the same code that you shared. Maybe we cant access JArray that way? I am clueless.
under .NET 4 or later and Newtonsoft 6.0.1 the code should work. Do you get a compile or runtime error?
my bad, I was using older JSON dll

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.