148

Does anyone know if there is application that will let me convert preferably XLS to JSON?

I'll also settle for a converter from CSV since that's what I'll probably end up having to write myself if there is nothing around.

Tony
  • 2,658
  • 2
  • 31
  • 46
mkoryak
  • 57,086
  • 61
  • 201
  • 257
  • How is the XLS structured? Do you assume that the first row are the column headers? – SheetJS Oct 19 '13 at 06:01
  • CSVkit does this: http://csvkit.readthedocs.org/en/latest/scripts/csvjson.html – Amanda Mar 04 '14 at 14:18
  • CsvCruncher takes CSV as a SQL table and let's you do a SELECT, exporting the result as CSV or JSON. https://github.com/OndraZizka/csv-cruncher – Ondra Žižka Oct 02 '18 at 03:51
  • This can be easily done using pandas dataframe. Import your csv to pandas dataframe and covert it to json. – R4444 Dec 19 '18 at 23:31
  • https://www.npmjs.com/package/convert-csv-to-json – Gene Bo Jul 19 '19 at 23:24
  • 1
    I'd suggest you look at [Data Transformer](https://geosystemsdev.com/products/data-transformer/) (disclaimer - I'm its developer). It converts between CSV, JSON, XML, and YML locally. It offers a number of conversion settings (with good defaults) so you can tailor the result for your purposes. You can get it from the [Mac App Store](https://apps.apple.com/us/app/data-transformer/id1490843070) or the [Microsoft Store](https://www.microsoft.com/en-au/p/data-transformer/9p2xp6nt2pv2). – Geo Systems Jan 01 '20 at 00:50

10 Answers10

199

You can try this tool I made:

Mr. Data Converter

It converts to JSON, XML and others.

It's all client side, too, so your data never leaves your computer.

Amanda
  • 12,099
  • 17
  • 63
  • 91
Shan Carter
  • 2,425
  • 2
  • 14
  • 10
  • 6
    It's fine but be aware it does not escape quotes correctly. When your CSV contains double quotes the output does not escape it. You might have to do it by hand. Very handy tool nontheless. – barrycarton Oct 29 '11 at 09:28
  • Hi @Shan Carter I want the user to just load excel file, Not to copy the content of excel, Is it possible with current code or I should fork it(If it is possible to do it). Please share your thoughts. – Rahul Gautam Aug 09 '13 at 09:42
  • To YAML? e.g. https://github.com/nodeca/js-yaml :D – sam Oct 01 '13 at 18:36
  • 2
    Awesome tool, @Shan Carter. I took the liberty to [fix a few things](https://github.com/thdoan/mr-data-converter) in my fork here: http://thdoan.github.io/mr-data-converter/ – thdoan Sep 19 '16 at 09:57
82

This worked perfectly for me and does NOT require a file upload:

https://github.com/cparker15/csv-to-json?files=1

robertc
  • 74,533
  • 18
  • 193
  • 177
zmonteca
  • 2,304
  • 1
  • 26
  • 26
46

Since Powershell 3.0 (shipped with Windows 8, available for Windows 7 and windows Server 2008 but not Windows Vista ) you can use the built-in convertto-json commandlet:

PS E:> $topicsjson = import-csv .\itinerary-all.csv | ConvertTo-Json 

PS E:\> $topicsjson.Length
11909

PS E:\> $topicsjson.getType()

IsPublic IsSerial Name                                     BaseType                  
-------- -------- ----                                     --------                  
True     True     Object[]                                 System.Array              

Online Help Page on Technet

knb
  • 9,138
  • 4
  • 58
  • 85
28

If you can't find an existing solution it's pretty easy to build a basic one in Java. I just wrote one for a client and it took only a couple hours including researching tools.

Apache POI will read the Excel binary. http://poi.apache.org/

JSONObject will build the JSON

After that it's just a matter of iterating through the rows in the Excel data and building a JSON structure. Here's some pseudo code for the basic usage.

FileInputStream inp = new FileInputStream( file );
Workbook workbook = WorkbookFactory.create( inp );

// Get the first Sheet.
Sheet sheet = workbook.getSheetAt( 0 );

    // Start constructing JSON.
    JSONObject json = new JSONObject();

    // Iterate through the rows.
    JSONArray rows = new JSONArray();
    for ( Iterator<Row> rowsIT = sheet.rowIterator(); rowsIT.hasNext(); )
    {
        Row row = rowsIT.next();
        JSONObject jRow = new JSONObject();

        // Iterate through the cells.
        JSONArray cells = new JSONArray();
        for ( Iterator<Cell> cellsIT = row.cellIterator(); cellsIT.hasNext(); )
        {
            Cell cell = cellsIT.next();
            cells.put( cell.getStringCellValue() );
        }
        jRow.put( "cell", cells );
        rows.put( jRow );
    }

    // Create the JSON.
    json.put( "rows", rows );

// Get the JSON text.
return json.toString();
Matt York
  • 301
  • 3
  • 3
  • If am having excel stored at E:/exceloutput.xlsx. What would be the values in file and inp in first two lines ? – Kate Aug 12 '16 at 07:42
13

This works for me and runs client-side: http://www.convertcsv.com/csv-to-json.htm

dataman
  • 131
  • 1
  • 5
6

I just found this:

http://tamlyn.org/tools/csv2json/

( Note: you have to have your csv file available via a web address )

DanDan
  • 101
  • 1
  • 4
6

Take a try on the tiny free tool:

http://keyangxiang.com/csvtojson/

It utilises node.js csvtojson module

Keyang
  • 1,828
  • 1
  • 12
  • 17
Keyang
  • 61
  • 1
  • 1
5

None of the existing solutions worked, so I quickly hacked together a script that would do the job. Also converts empty strings into nulls and and separates the header row for JSON. May need to be tuned depending on the CSV dialect and charset you have.

#!/usr/bin/python
import csv, json
csvreader = csv.reader(open('data.csv', 'rb'), delimiter='\t', quotechar='"')
data = []
for row in csvreader:
    r = []
    for field in row:
        if field == '': field = None
        else: field = unicode(field, 'ISO-8859-1')
        r.append(field)
    data.append(r)
jsonStruct = {
    'header': data[0],
    'data': data[1:]
}
open('data.json', 'wb').write(json.dumps(jsonStruct))
Tronic
  • 10,250
  • 2
  • 41
  • 53
  • Is this related to http://johntron.com/creations/csv-to-json/ – David Oct 04 '11 at 00:16
  • No, except that the same libraries (csv, json) are being used. I wrote my code from scratch. The loops are required for the processing I wanted to do (charset conversion and replacing empty strings with null). – Tronic Oct 08 '11 at 19:12
  • 1
    I'm wondering about the conflict between many of the comments which say "this worked for me" and this one claiming that "none of the existing solutions worked." – B. Clay Shannon-B. Crow Raven Jun 14 '13 at 16:41
4

Instead of hard-coded converters, how about CSV support for Jackson (JSON processor): https://github.com/FasterXML/jackson-dataformat-csv. So core Jackson can read JSON in as POJOs, Maps, JsonNode, almost anything. And CSV support can do the same with CSV. Combine the two and it's very powerful but simple converter between multiple formats (there are backends for XML, YAML already, and more being added).

An article that shows how to do this can be found here.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
4

See if this helps: Back to CSV - Convert CSV text to Objects; via JSON

This is a blog post published in November 2008 that includes C# code to provide a solution.

From the intro on the blog post:

As Json is easier to read and write then Xml. It follows that CSV (comma seperated values) is easier to read and write then Json. CSV also has tools such as Excel and others that make it easy to work with and create. So if you ever want to create a config or data file for your next app, here is some code to convert CSV to JSON to POCO objects

qxotk
  • 2,384
  • 5
  • 24
  • 39