JavaScript SSV To JSON Convertor

This script converts Tab Separated Values (TSV), Comma Separated Values (CSV), or similar String Separated Values (SSV) into JavaScript Object Notation (JSON).

It expects a single header row, the column headers in which are used to create property names in the JSON output (_ is substituted for illegal characters).  Additionally you must specify the variable name for the entire file as part of your input.  You must ensure that there are no examples of separators inside quoted strings within the data.

The optional numbers list is a comma separated list of column numbers whose data is to be treated as numeric.

The optional exclusions list is a comma separated list of column numbers which are to be omitted from the output.

You can specify the separator character, string, or regular expression.  Default is a \t, \x09, <TAB>, <Ctrl-I> character.  You can use JavaScript escapes to specify unprintable characters, or regular expressions (but do not include the latter's bounding slashes, and sometimes you may need to double-escape some characters, for example if \Character doesn't work, try \\Character).

By default a comment,"Created by: <This URL>, <Date>", will be added to the output.  This can be edited, or omitted altogether by blanking it.

As long as source data and comments are not mixed on the same line, the convertor can copy existing JavaScript comments, denoted by // or /* */, &/or convert a choice of other comments from standard filetypes, or those specified by a Regular Expression (RE)  The following table describes this functionality:

Online Version Download Version Description
No comments   Treat all source as data.
Use this RE -C:<RE> Convert comments captured by the specified RE and copy existing JS comments.  If RE is blank, no conversion occurs but existing JS comments are copied.  If RE is a single character, an RE is constructed around it to convert single line comments denoted by the specified character being the leading non-blank character.
Assembler/INI/REG -C:; Convert single line comments denoted by a leading ; and copy existing JS comments.
Basic/VB -C:' Convert single line comments denoted by a leading ' and copy existing JS comments.
Markup -C:m Convert multi line markup comments denoted by <!-- --> and copy existing JS comments.
Properties -C:# Convert single line comments denoted by a leading # and copy existing JS comments.

To use this online version, paste your SSV data into the input, set the parameters, click Submit, and copy the resulting JSON output.  To download the console script to your PC, <Right-Click> SSV2JSON.js and choose Save As.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Updated Description
15/01/2011 Corrected lack of quoting on property names that causes errors with some parsers.
13/06/2010 v1.1  -  Safer Array for() loops if the Array.prototype is altered.
09/12/2009 Created v1.