In my recent post about JSON Table Editor, I listed reading CSV under feature to-do list. Working on it, I remember that I have blogged about reading CSV in the past.
So I ported the code from C# to JavaScript. The porting process is very much painless since C# and JavaScript has the same dialect. The JavaScript implementation is much simpler compared to C# because JavaScript is a dynamic language. You can compare the code side by side if you think I bluffed you :D.
Please understand that I wrote the C# implementation way back when I was only have Visual Studio 2005 in my workstation. So sadly, no anonymous object for me that time.
You could argue why complicated method is being used instead of just splitting the string using comma, but simple splitting won’t work when the CSV contains comma inside brackets.
Anyway, enjoy the code and have fun coding.
Cheers!
var CsvDataSource = {
cleanQuotes : function (raw) {
//Remove extra quotes/spaces in the beginning and end of string
//Parse the string into Boolean or Number
var str = raw;
while (str.charAt(0) == '"' && str.charAt(str.length - 1) == '"')
str = str.substr(1, str.length - 2);
if (str.trim)
str = str.trim();
if (str == "true")
return true;
else if (str == "false")
return false;
else if (Number(str))
return Number(str);
else
return str;
},
csvSplits : function (data, separatorChar) {
//Split a string using the specified separator character. Will keep value between quotes
//as a single value
var me = CsvDataSource;
var result = [];
var lastpost = 0;
var insideQuote = false;
for (var i = 0; i < data.length; i++) {
if (data.charAt(i) == separatorChar) {
if (!insideQuote) {
var str = me.cleanQuotes(data.substr(lastpost, i - lastpost));
result.push(str);
lastpost = i + 1;
}
if (i > 0 && i < data.length - 1) {
if (data.charAt(i - 1) == '"' && insideQuote) {
var str = me.cleanQuotes(data.substr(lastpost, i - lastpost));
result.push(str);
lastpost = i + 1;
insideQuote = false;
}
if (data.charAt(i + 1) == '"' && !insideQuote)
insideQuote = true;
}
}
}
result.push(me.cleanQuotes(data.substr(lastpost)));
return result;
},
readCSV : function (rawText, separatorChar) {
var result = [];
var lines = rawText.split('\n');
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (Ext.String.trim(line) != '') {
var values = this.csvSplits(line, separatorChar);
result.push(values);
}
}
return result;
}
}
Update 8th Feb 2013
Removed the opening and closing quotes in CSV values
Try to convert the String value into Boolean or Number, if failed, remains as a String
About Hardono
Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.
With somanyJSONEditor tools available online, I still can’t find a decent editor with Grid interface.
So I decided to create my own. Considering my sloppy Sencha ExtJS knowledge, I conclude it should not be that difficult to implement with ExtJS. As a bonus, I can get my hand dirty with ExtJS 4.
Paste JSON/CSV text in the first textarea. Please note that for CSV, the first line will be used as column name.
Click Load Model button. If failed, please fix the text to follow the correct JSON/CSV format. Note that you could change the default CSV separator character in the narrow textbox before the ‘Load JSON’ button.
Add new record, or edit existing one in the Table Editor grid
Click Output to JSON button to get the final JSON array object
Editing Remote JSON/CSV
Type the URL of JSON/CSV in the textbox below the first text area.
Click Load Model button. If failed, edit the JSON/CSV top text area, clear the URL, then click ‘Load JSON’ button. If you didn’t clear the URL textbox, it will always try retrieve the file and ignore your modified version.
Paste values from HTML/Excel
Currently, the paste action is only available in Chrome, Safari and Opera. For now, only single column values is accepted.
The values will be separated by newline character.
Please ensure you select the appropriate Paste method before pasting.
In Insert mode (Overwrite checkbox is unchecked), multiple copies of the selected row will be inserted below it. The number of copies will depend on the number of pasted values. These row copies will then have their corresponding column updated with the pasted value.
In Overwrite mode , the values of the current cell and any cells below it will replaced. If the number of pasted values exceed the last row, Insert mode will be applied to the last row using the remaining pasted values
To paste values, select the cell. Then press Ctrl+V
Editing complex JSON object
Complex object here is referring to any other JavaScript type that is not Number, Boolean, String and Date. Imagine you have the following JSON that you want to edit (using John Bevilaqua‘s data)
Konstantin Portnov: When handling unicode, the unicode characters shown as escape characters link
Do you have any other suggestions of functionality that would improve JSON Table Editor? Did you spot any bugs?
About Hardono
Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.
I previously created a post about this topic using YUI. But now that I’ve completed many projects using Ext.NET/Sencha ExtJS, I feel I can complete the same thing faster and simpler using ExtJS.
Compared to the YUI version, the ExtJS version is better because we can do filtering on each column. The code also much cleaner in ExtJS version.
I also updated the data. I removed codaset because they no longer online. I also added a new favourite of mine, BitBucket.
I hope it’s useful 🙂
About Hardono
Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.