0

I try to use an AJAX request with datatable, but my JSON has the wrong format. So I try to convert the structure. I have a JSON string as an array of objects like this form:

[  
    {  
          "timestamp":"1461308319473",
          "id":"1",
          "value":"77"
    },
    {  
          "timestamp":"1461308458181",
          "id":"2",
          "value":"99"
    }
] 

But I need this structure:

{
  "data": [
    [
      "1461308319473",
      "1",
      "77"
    ],
    [
      "1461308458181",
      "2",
      "99"
    ]
  ]
}

How can I convert the arrays? I would like to use datatable with this command:

$("#table")
    .dataTable({
        "ajax": {
            "type": "GET",
            "url": "https://url",
            "dataSrc": function (jsonData) {
            for (var i = 0; i < jsonData.length; i++) {
                returnData.push({
                    'timestamp': moment.unix(jsonData[i].timestamp / 1000).format("DD.MM.YY hh:mm:ss"),
                    'id': jsonData[i].id,
                    'value': jsonData[i].value
                });
            }
            return returnData;
        },
        "columns": [
            { "data": "timestamp" },
            { "data": "id" },
            { "data": "value" }
        ]
    }
});

At moment I get the following error: enter image description here

Thanks

cSteusloff
  • 2,487
  • 7
  • 30
  • 51
  • Possible duplicate of [a better way to convert JS object to array](http://stackoverflow.com/questions/6857468/a-better-way-to-convert-js-object-to-array) – layonez May 18 '16 at 08:55

3 Answers3

1

you can do something like:

var keys = ['timestamp', 'id', 'value'],
    data = jsonData.map(function(datum){
      return keys.map(function(key){return datum[key]})      
    })

data  = {data:data}
console.log(data)

in ES6:

let keys = ['timestamp', 'id', 'value'],
  data = jsonData.map( datum => keys.map(key => datum[key]))

data = {data}

Fiddle Demo

mido
  • 24,198
  • 15
  • 92
  • 117
1

If you are sue that there are only timestamp, id and value then

var data = [ 
    {  
          "timestamp":"1461308319473",
          "id":"1",
          "value":"77"
    },
    {  
          "timestamp":"1461308458181",
          "id":"2",
          "value":"99"
    }
] ;

var result = {};
result.data = data.map(function(d) {
    return [
        d.timestamp,
        d.id,
        d.value
    ];
});

document.write(['<pre>', JSON.stringify(result, 0, 3), '</pre>'].join(''));
Yuriy Yakym
  • 3,616
  • 17
  • 30
0

You can use the following example to create an object that has a data property which is an empty array and then use the forEach() method on your array to populate as such:

var objectWithArray = {'data': []};    
var arrWithObject = [  
        {  
              "timestamp":"1461308319473",
              "id":"1",
              "value":"77"
        },
        {  
              "timestamp":"1461308458181",
              "id":"2",
              "value":"99"
        }
    ] 
    arrWithObject.forEach(functio(v,i){
        objectWithArray.data.push([v.timespam, v.id, v.value])
    })