Skip to content Skip to sidebar Skip to footer

How To Convert Json To Array In Javascript

I want to convert JSON to Array and I return value by : console.log(data); value is : so, I converted to JSON by: console.log(JSON.stringify(data, null, ' ')); value is : I w

Solution 1:

You can simply try on followings:

var arr = Object.keys(obj).map(function(k) { return obj[k] });

Solution 2:

The syntax of your expected output is incorrect as you cant have object without key value pair.

you can have your output as

{"data":[[1,"Alex",20]]},{"data":[[2,"Zara",18],[2,"Zara",19]]}

here is the solution considering above output

var inputArr = [
  {
    "data" : [
          {
            "month" : 1,
             "name" : "Alex",
             "sum" : 20
          }
      ]  
  },
  {
    "data" : [
          {
            "month" : 2,
             "name" : "Zara",
             "sum" : 18
          },
          {
            "month" : 2,
            "name" : "Zara",
            "sum" : 19
          }
      ]  
  }
];

inputArr.forEach(function(item){
    for (var i=0; i< item.data.length; i++){
        var myArr = [];
        myArr.push(item.data[i].month);
        myArr.push(item.data[i].name);
        myArr.push(item.data[i].sum);
        item.data[i] = myArr;
    }
})

console.log(JSON.stringify(inputArr));

NOTE: Solution can be simplified if you are Ok to use ES6 in your code.

Solution 3:

Here you go with a solution (with jQuery) https://jsfiddle.net/mhhqj1nc/

var data = [
  {
    "data" : [
          {
            "month" : 1,
             "name" : "Alex",
             "sum" : 20
          }
      ]  
  },
  {
    "data" : [
          {
            "month" : 2,
             "name" : "Zara",
             "sum" : 18
          },
          {
            "month" : 2,
            "name" : "Zara",
            "sum" : 19
          }
      ]  
  }];

var newdata = [];

$.each(data, function(i, v){
  newdata.push({data: []});
  var temp = [];
  $.each(v.data, function(k, val){
    var keys = Object.keys(v.data[k]);
    $.each(keys, function(j){
      temp.push(v.data[k][keys[j]]);
    });
    newdata[i].data.push(temp);
    temp = [];
  });
});
console.log(JSON.stringify(newdata));
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Your expected output is invalid.

Expected output should be

[{
  "data": [1, "Alex", 20]
 }, {
  "data": [
    [2, "Zara", 18],
    [2, "Zara", 19]
  ]
}]

Here you go with a solution (with vanilla JavaScript) https://jsfiddle.net/mhhqj1nc/1/

var data = [
  {
    "data" : [
          {
            "month" : 1,
             "name" : "Alex",
             "sum" : 20
          }
      ]  
  },
  {
    "data" : [
          {
            "month" : 2,
             "name" : "Zara",
             "sum" : 18
          },
          {
            "month" : 2,
            "name" : "Zara",
            "sum" : 19
          }
      ]  
  }];

var newdata = [];

for(var i=0; i<data.length; i++){
  newdata.push({data: []});
  for(var j=0; j<data[i].data.length; j++){
    var keys = Object.keys(data[i].data[j]);
    var temp = [];
    for(var k in keys){
      temp.push(data[i].data[j][keys[k]]);
    }
    newdata[i].data.push(temp);
  }
}

console.log(JSON.stringify(newdata));

Hope this will help you.

Post a Comment for "How To Convert Json To Array In Javascript"