Convert data format: JSON to CSV

Hi guys!

Is there a way to convert the exported JSON created through Data Acquisition to CSV or directly download it in CSV?

Thank you in advance! :smiley:

@juacuegut, sure, you can do something like this (in Node.js):

let json = {
    "protected": {
        "ver": "v1",
        "alg": "HS256",
        "iat": 1564128599
    },
    "signature": "b0ee0572a1984b93b6bc56e6576e2cbbd6bccd65d0c356e26b31bbc9a48210c6",
    "payload": {
        "device_name": "ac:87:a3:0a:2d:1b",
        "device_type": "DISCO-L475VG-IOT01A",
        "interval_ms": 10,
        "sensors": [
            { "name": "accX", "units": "m/s2" },
            { "name": "accY", "units": "m/s2" },
            { "name": "accZ", "units": "m/s2" }
        ],
        "values": [
            [ -9.81, 0.03, 1.21 ],
            [ -9.83, 0.04, 1.27 ],
            [ -9.12, 0.03, 1.23 ],
            [ -9.14, 0.01, 1.25 ]
        ]
    }
};

// headers are the timestamp and then the sensor names
console.log('timestamp,' + (json.payload.sensors.map(s => s.name).join(',')));

// loop through the values
for (let vix = 0; vix < json.payload.values.length; vix++) {
    // we start with the current timestamp
    let line = json.payload.interval_ms * vix + ',';
    // values is an array of either: numbers, or an array of numbers
    if (json.payload.values[vix] instanceof Array) {
        line += json.payload.values[vix].join(',');
    }
    else {
        line += json.payload.values[vix]
    }
    console.log(line);
}

This outputs:

timestamp,accX,accY,accZ
0,-9.81,0.03,1.21
10,-9.83,0.04,1.27
20,-9.12,0.03,1.23
30,-9.14,0.01,1.25
1 Like

Thank you so much @janjongboom :smiley: