Related Tags:
JQuery JQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. Learn More, JavaScript Javascript is an object-oriented computer programming language commonly used to create interactive effects within web browsers. Learn More, CSV A CSV is a comma-separated values file, which allows data to be saved in a tabular format. CSVs look like a garden-variety spreadsheet but with a . csv extension. CSV files can be used with most any spreadsheet program, such as Microsoft Excel or Google Spreadsheets. Learn More, Microsoft Excel Microsoft Excel is a spreadsheet developed by Microsoft for Windows, macOS, Android and iOS. It features calculation, graphing tools, pivot tables, and a macro programming language called Visual Basic for Applications. Learn More,

Having issues after converting json to csv, languages other than English encoded as special characters also mobile number displayed as exponential values

Description:I have used the javascript code for the conversion of json object to csv file and it was working fine when I have one language(English) but when i have added another language Arabic in my case , the generated csv showing special characters instead of Arabic characters and also mobile numbers are always displayed as exponential values in csv file.

Posted by: | Posted on: Dec 18, 2019

1 answers

Replies

3

Hi junaid,
I understood your issues your first issue is an encoding issue for which you just need to update "csvEncoding" in that code and add "%EF%BB%BF" which will solve your first issue, now about your second issue it is due to an usual behavior of csv files, which displays large numbers in that exponential value,For a workaround to solve that you need to append '\u200C' character to specifically to your mobile number field or you can add '\u200C' (as it is an empty character so it will not effect any value, it will just display them as string) character to all your fields if you want to display them all as string. so after making these updates your code will be updated as follows.

/**
@namespace Converts JSON to CSV.
Compress with: http://jscompress.com/
*/
(function (window) {
"use strict";
/**
Default constructor
*/
var _CSV = function (JSONData) {
if (typeof JSONData === 'undefined')
return;

var csvData = typeof JSONData != 'object' ? JSON.parse(settings.JSONData) : JSONData,
csvHeaders,
csvEncoding = 'data:text/csv;charset=utf-8,%EF%BB%BF',
csvOutput = "",
csvRows = [],
BREAK = '\r\n',
DELIMITER = ',',
FILENAME = "export.csv";

// Get and Write the headers
csvHeaders = Object.keys(csvData[0]);
csvOutput += csvHeaders.join(',') + BREAK;

for (var i = 0; i < csvData.length; i++) {
var rowElements = [];
for(var k = 0; k < csvHeaders.length; k++) {
rowElements.push('\u200C' +csvData[i][csvHeaders[k]]);
} // Write the row array based on the headers
csvRows.push(rowElements.join(DELIMITER));
}
csvOutput += csvRows.join(BREAK);
// Initiate Download
var a = document.createElement("a");

if (navigator.msSaveBlob) { // IE10
navigator.msSaveBlob(new Blob([csvOutput], { type: "text/csv" }), FILENAME);
} else if ('download' in a) { //html5 A[download]
a.href = csvEncoding + encodeURIComponent(csvOutput);
a.download = FILENAME;
document.body.appendChild(a);
setTimeout(function() {
a.click();
document.body.removeChild(a);
}, 66);
} else if (document.execCommand) { // Other version of IE
var oWin = window.open("about:blank", "_blank");
oWin.document.write(csvOutput);
oWin.document.close();
oWin.document.execCommand('SaveAs', true, FILENAME);
oWin.close();
} else {
alert("Support for your specific browser hasn't been created yet, please check back later.");
}
};
window.CSVExport = _CSV;
})(window);

Replied by: | Replied on: Dec 20, 2019



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview