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

1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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(''));

Comments

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])
    })

Comments

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.