There is json: enter image description here

It is necessary to export it to .csv (or to any other format that an exel could open without problems). Preferably without using third-party libraries.

  • What is csv? Just a set of values ​​with delimiters, so just overtake these objects into lines with a separator and save with the extension .csv - yolosora
  • one
    csv is a set separating cells ; and \n what exactly is the problem? - Artem Gorlachev
  • Those. I first make a delimited string from json in a loop, then convert and download? - r.mcreal
  • Yes, in the loop you create text content csv, then you’ll think how to force the browser to give it with a file) - Artem Gorlachev
  • And do not forget to screen ; in your data! - Stepan Kasyanenko

1 answer 1

  1. Create an array of strings in which the field values ​​of the objects are separated ; or , 1 (and with the addition of each line terminating \r\n ).
    2 if necessary, enclose the value in double quotes

  2. Create an instance of Blob by passing the generated array to the constructor and entering text/csv in the type parameter string (MIME type)

  3. Get the URL of a Blob instance with the static URL.createObjectURL() method

  4. Do whatever you want with the URL.


1 Excel uses a comma as a delimiter.
2 In the case when the value is a string, and contains characters separator or newline. If the value also contains quotes, then they must be doubled: "qwe ""rty"" uio" - this can be done using the String.replace() method.


 let data = rndObjArr(50), csvRows = [], blob; data.forEach(o => csvRows.push(`${o.date},${o.count}\r\n`)); blob = new Blob(csvRows, { type: 'text/csv;charset=utf-8;' }); document.body.insertAdjacentHTML('beforeend', ` <a href="${URL.createObjectURL(blob)}" download="export.csv">Скачать CSV</a> `); function rndObjArr(size) { const rndInt = (from, to) => Math.floor(Math.random() * (to - from)) + from; let result = [], dateStr; while (size--) { dateStr = `${rndInt(2010, 2019)}-${String(rndInt(1, 13)).padStart(2, '0')}-${String(rndInt(1, 31)).padStart(2, '0')}`; result.push({ date: dateStr, count: rndInt(0, 50) }); } return result; }