Super Pesucen
Super Pesucen

Reputation: 21

Change structure of JSON

I want to bind data from the database to a line chart, but I'm having difficulty making the JSON format. Please help me to change JSON like this:

{
  "LineListChart": [
    {
      "name": "Pembunuhan",
      "data": 0
    },
    {
      "name": "Pembunuhan",
      "data": 0
    },
    {
      "name": "Pembunuhan",
      "data": 14
    },
    {
      "name": "Pembunuhan",
      "data": 4
    },
    {
      "name": "Pembunuhan",
      "data": 10
    }
   ]
}

to be like this:

{
  "LineListChart": [
    {
      "name": "Pembunuhan",
      "data": [0,0,14,4,10]
    }
   ]
}

Upvotes: 0

Views: 310

Answers (3)

Hien Nguyen
Hien Nguyen

Reputation: 18975

Because the question is json.net, I provide an answer by C# with json.net

    string test = @"{
                'LineListChart': [
                {
                    'name': 'Pembunuhan',
                    'data': 0
                },
                {
                    'name': 'Pembunuhan',
                    'data': 0
                },
                {
                    'name': 'Pembunuhan',
                    'data': 14
                },
                {
                    'name': 'Pembunuhan',
                    'data': 4
                },
                {
                    'name': 'Pembunuhan',
                    'data': 10
                }
                ]
            }";

   JObject obj = JObject.Parse(test);

   JArray categories = (JArray)obj["LineListChart"];

   var query = from c in categories group c by c["name"]
                into g select new { name = g.Key.ToString(), 
                     data =g.Select(c => (string)c["data"]) };

   var res = JsonConvert.SerializeObject(new { LineListChart = query.ToList() });

Upvotes: 1

Brian Rogers
Brian Rogers

Reputation: 129677

Using Json.Net's LINQ-to-JSON API you can transform your JSON like this:

var obj = JObject.Parse(json);
obj["LineListChart"] = new JArray(
    obj["LineListChart"]
    .Children<JObject>()
    .GroupBy(jo => (string)jo["name"], jo => jo["data"])
    .Select(g => new JObject(
        new JProperty("name", g.Key),
        new JProperty("data", new JArray(g))
    ))
);
json = obj.ToString();

Fiddle: https://dotnetfiddle.net/qpuQy7

Upvotes: 2

Alen.Toma
Alen.Toma

Reputation: 4870

you could use reduce have a look below

var data ={
  "LineListChart": [
    {
      "name": "Pembunuhan",
      "data": 0
    },
    {
      "name": "Pembunuhan",
      "data": 0
    },
    {
      "name": "Pembunuhan",
      "data": 14
    },
    {
      "name": "Pembunuhan",
      "data": 4
    },
    {
      "name": "Pembunuhan",
      "data": 10
    }
   ]
}

var chartData= data.LineListChart.reduce((result, item)=> {
if (!result.LineListChart){
     result.LineListChart =[{name: item.name, data: [item.data]}];
     }else result.LineListChart[0].data.push(item.data);
    
    return result;
}, {})

console.log(chartData)

Upvotes: 2

Related Questions