3

I want to convert JSON to Array and I return value by : console.log(data);

value is :

[{ "data" : [  [object]  ] },
[{ "data" : [  [object] , [object]  ] }

so, I converted to JSON by:

console.log(JSON.stringify(data, null, "    "));

value is :

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

]

I want convert to Array :

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

How to convert ?

jane
  • 67
  • 1
  • 1
  • 8
  • 3
    Possible duplicate of [Converting JSON Object into Javascript array](https://stackoverflow.com/questions/20881213/converting-json-object-into-javascript-array) – Ryan Turnbull Aug 29 '17 at 04:34
  • Possible duplicate of [Converting a JS object to an array](https://stackoverflow.com/questions/6857468/converting-a-js-object-to-an-array) – samAlvin Aug 29 '17 at 04:47
  • Your expected `JSON` is invalid. – Shiladitya Aug 29 '17 at 05:06
  • `"data" :{ [ 1, "Alex", 20 ] }` - this looks like bad syntax, you probably need to learn basics first. you probably need `data: [ 1, "Alex", 20 ]` – Max Deepfield Aug 29 '17 at 09:18

3 Answers3

9

You can simply try on followings:

var arr = Object.keys(obj).map(function(k) { return obj[k] });
Kunvar Singh
  • 1,779
  • 2
  • 13
  • 21
1

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.

Nemani
  • 778
  • 5
  • 12
0

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));
<script src="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.

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
  • thanks for you answer but I try in filename.js it's return $ is not defined. I wrong ? – jane Aug 29 '17 at 05:27
  • @jane You forgot to add `jQuery` library file. Add `` in the head section – Shiladitya Aug 29 '17 at 05:28
  • I use node.js and I try var jquery = require('jquery'); it's return $ is not defined again. – jane Aug 29 '17 at 06:52
  • help me again , please. https://stackoverflow.com/questions/45952057/how-to-convert-data-table-to-json-in-javascript – jane Aug 30 '17 at 08:23