Ad

How To Convert JSON To CSV Using JavaScript

- 1 answer

I am trying to convert a JSON object to CSV with JavaScript, but I am not getting the expected results.

EDIT: My JSON object is stored in a variable, so every time i access 'list', it comes out undefined. Is there a way I can access the list through a variable?

This is my JSON object:

{
  "type": "success",
  "message": "success",
  "list": [
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "testMendoza",
      "CocNumber": "1000000001",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 1
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "ff",
      "CocNumber": "1000000002",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 2
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "c",
      "CocNumber": "1000000003",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 3
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000004",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 4
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000005",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 5
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000006",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 6
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000007",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 7
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000008",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 8
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000009",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 9
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000010",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 10
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000011",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 11
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000012",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 12
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000013",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 13
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000014",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 14
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000015",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 15
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000016",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 16
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000017",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 17
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000018",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 18
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000019",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 19
    },
    {
      "ExtensionData": {},
      "Address": "Baler",
      "fld_test": "test",
      "CocNumber": "1000000020",
      "CustomerName": "test",
      "IssueDate": "\\/Date(1584892800000)\\/",
      "ProductName": "testProd",
      "TransactionId": 20
    }
  ],
  "totalPage": 0
}

I have tried with the following JavaScript function, but the CSV comes out empty:

function ConvertToCSV(objArray) {
    alert("start json to csv conversion");
    var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
    var str = '';

    for (var i = 0; i < array.length; i++) {
        var line = '';
        for (var index in array[i]) {
            if (line != '') line += ','

            line += array[i][index];
        }

        str += line + '\r\n';
        //
    }
    return str;
}

The JSON code is valid according to online formatters, so is there something wrong with the JavaScript?

Ad

Answer

you can find a great answer here - https://stackoverflow.com/a/31536517/7802051

and a small example for you:

const data = {"type":"success","message":"success","list":[{"ExtensionData":{},"Address":"Baler","fld_test":"test Mendoza","CocNumber":"1000000001","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":1},{"ExtensionData":{},"Address":"Baler","fld_test":"ff","CocNumber":"1000000002","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":2},{"ExtensionData":{},"Address":"Baler","fld_test":"c","CocNumber":"1000000003","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":3},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000004","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":4},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000005","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":5},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000006","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":6},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000007","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":7},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000008","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":8},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000009","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":9},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000010","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":10},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000011","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":11},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000012","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":12},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000013","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":13},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000014","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":14},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000015","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":15},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000016","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":16},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000017","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":17},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000018","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":18},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000019","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":19},{"ExtensionData":{},"Address":"Baler","fld_test":"test","CocNumber":"1000000020","CustomerName":"test","IssueDate":"/Date(1584892800000)/","ProductName":"testProd","TransactionId":20}],"totalPage":0}


const items = data.list
const replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values here
const header = Object.keys(items[0])
let csv = items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
csv.unshift(header.join(','))
csv = csv.join('\r\n')

console.log(csv)

Ad
source: stackoverflow.com
Ad