Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow collapsing json output and fix bug with quotes within a value #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ application/cache/*

application/logs/*
!application/logs/index.html
!application/logs/.htaccess
!application/logs/.htaccess

# Intellij IDEA
.idea
*.iml
10 changes: 7 additions & 3 deletions application/views/csv2json_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<p>Convert your CSV or TSV formatted data to JSON. Optionally transpose your data, or output an object instead of an array. Copy/paste or upload your Excel data to convert it to JSON.</p>
</div>
</div>

<div class="row">
<div class="col-md-5 more-bottom-margin">
<div class="form-group">
Expand Down Expand Up @@ -40,7 +40,7 @@
<i class="glyphicon glyphicon-remove"></i> Clear
</button>
</div>

<div class="col-md-7 more-bottom-margin">
<div class="form-group">
<label>Options <small>Hover on option for help</small></label>
Expand All @@ -62,10 +62,14 @@
<label class="inline" title="Transpose the data beforehand.">
<input type="checkbox" id="transpose" name="transpose" class="save" /> Transpose
</label>
&nbsp;
&nbsp;<label class="inline" title="Collapse.">
<input type="checkbox" id="collapse" name="collapse" class="save" /> Collapse
</label>
<span>&nbsp;</span>
<label class="inline">Output:</label>
<label class="radio-inline" title="Output an array of objects."><input type="radio" id="output-array" name="output" class="save" value="array" checked="checked" />Array</label>
<label class="radio-inline" title="Output an object instead of an array. First column is used as hash key."><input type="radio" id="output-hash" name="output" class="save" value="hash" />Hash</label>

</div>
</div>
<div class="form-group code-group">
Expand Down
21 changes: 11 additions & 10 deletions js/csvjson/csv2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
* - separator: Optional. Character which acts as separator. If omitted,
* will attempt to detect comma (,), semi-colon (;) or tab (\t).
*
* Dependencies:
* Dependencies:
* - underscore (http://underscorejs.org/)
* - underscore.string (https://github.com/epeli/underscore.string)
*
* Copyright (c) 2014 Martin Drapeau
*
*/

var errorDetectingSeparator = "We could not detect the separator.",
errorEmpty = "Please upload a file or type in something.",
errorEmptyHeader = "Could not detect header. Ensure first row cotains your column headers.",
separators = [",", ";", "\t"];

function detectSeparator(csv) {
var counts = {},
sepMax;
Expand Down Expand Up @@ -103,6 +103,7 @@
} else {

// We found a non-quoted value.
// but escape any internal quotes
strMatchedValue = arrMatches[ 3 ];

}
Expand All @@ -116,11 +117,11 @@
// Return the parsed data.
return( arrData );
}

function convert(csv, options) {
options || (options = {});
if (csv.length == 0) throw errorEmpty;

var separator = options.separator || detectSeparator(csv);
if (!separator) throw errorDetectingSeparator;

Expand All @@ -134,13 +135,13 @@
keys = _.map(keys, function(key) {
return _(key).chain().trim().trim('"').value();
});

var json = options.hash ? {} : [];
for (var l = 0; l < a.length; l++) {
var row = {},
hashKey;
for (var i = 0; i < keys.length; i++) {
var value = _(a[l][i]).chain().trim().trim('"').value(),
var value = _(a[l][i]).chain().trim().value(),
number = value === "" ? NaN : value - 0;
if (options.hash && i == 0)
hashKey = value;
Expand All @@ -152,11 +153,11 @@
else
json.push(row);
}

return json;
};

this.CSVJSON || (this.CSVJSON = {});
this.CSVJSON.csv2json = convert;

}).call(this);
20 changes: 12 additions & 8 deletions js/src/csv2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright (c) 2014 Martin Drapeau
*/
APP.csv2json = function() {

var uploadUrl = '/csv2json/upload',
sepMap = {
comma: ',',
Expand All @@ -15,37 +15,41 @@ APP.csv2json = function() {
$separator = $('select[name=separator]'),
$parseNumbers = $('input[type=checkbox][name=parseNumbers]'),
$transpose = $('input[type=checkbox][name=transpose]'),
$collapse = $('input[type=checkbox][name=collapse]'),
$output = $('input[type=radio][name=output]'),
$csv = $('#csv'),
$json = $('#json'),
$clear = $('#clear, a.clear'),
$convert = $('#convert, a.convert');

$convert.click(function(e) {
e.preventDefault();

var csv = _.trim($csv.val()),
separator = $separator.find('option:selected').val(),
options = {
transpose: $transpose.is(':checked'),
collapse: $collapse.is(':checked'),
hash: $output.filter(':checked').val() == 'hash',
parseNumbers: $parseNumbers.is(':checked')
parseNumbers: $parseNumbers.is(':checked'),
},
json;
if (separator != 'auto') options.separator = sepMap[separator];

try {
console.log(options);
json = CSVJSON.csv2json(csv, options);
} catch(error) {
APP.reportError($json, error);
return false;
}

var result = JSON.stringify(json, null, 2);

var result = options.collapse
? JSON.stringify(json)
: JSON.stringify(json, null, 2);
$json.removeClass('error').val(result);
});

APP.start({
$convert: $convert,
$clear: $clear,
Expand Down