Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting JSON object to CSV format in JavaScript

I am trying to convert a JavaScript object set in to CSV format

You can get the idea about my Javascript object, if you put it in online JSON parser https://jsonformatter.org/json-parser

This is how I tried to work it out... but it flopped.. http://jsfiddle.net/fHQzC/11/

I am trying to take the whole values corresponding to the value "term" and corresponding title in to CSV format

The expected output for is like

Time,Dec 9, 2012  News,Germany,election, Egypt,Revolution, Japan, Earthquake Person,Obama, Beckham Title,Pearce Snubs Beckham                                 Time,Dec 5, Birthday Person, Lebron James News,Italy,Euro 2012 Final Title-Heats National Champions                                

and is it possible to download the csv file in excel sheet the one I found in Stackoverflow was not really useful me...

like image 885
user1371896 Avatar asked Jun 29 '12 06:06

user1371896


People also ask

Can we convert JSON to CSV in Java?

We can convert a JSON Array to CSV format using org. json. CDL class, it can provide a static method toString(), to convert a JSONArray into comma-delimited text.

What is toJSON () in JSON?

if you need to read or clone all of a model's data attributes, use its toJSON() method. This method returns a copy of the attributes as an object (not a JSON string despite its name). (When JSON.


2 Answers

you can try as

$(document).ready(function () {          // Create Object         var items = [               { name: "Item 1", color: "Green", size: "X-Large" },               { name: "Item 2", color: "Green", size: "X-Large" },               { name: "Item 3", color: "Green", size: "X-Large" }];          // Convert Object to JSON         var jsonObject = JSON.stringify(items);          // Display JSON         $('#json').text(jsonObject);          // Convert JSON to CSV & Display CSV         $('#csv').text(ConvertToCSV(jsonObject));     }); 

and a function ConvertToCSV

// JSON to CSV Converter         function ConvertToCSV(objArray) {             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;         } 

Source

like image 200
Hemant Metalia Avatar answered Sep 25 '22 00:09

Hemant Metalia


Probably more elegant and the simplest solution

function convertToCSV(arr) {   const array = [Object.keys(arr[0])].concat(arr)    return array.map(it => {     return Object.values(it).toString()   }).join('\n') }   console.log(   convertToCSV(     [       {         id: 1,         name: 'Foo',         timestamp: new Date()       },       {         id: 2,         name: 'Bar',         timestamp: new Date()       },       {         id: 3,         name: 'Baz',         timestamp: new Date()       }     ]   ) )
like image 29
eeqk Avatar answered Sep 25 '22 00:09

eeqk