Search code examples
jsonexcelemeditor

How to convert JSON file to excel?


I have a large JSON file around 3gb and I want to convert it to readable excel format. How can I achieve this?

Can I do it via emeditor? It supports JSON but not sure if I can convert that to excel.

Any input is appreciated. Thanks

For eg:

{
  "name": "George Washington",
  "birthday": "February 22, 1732",
  "address": "Mount Vernon, Virginia, United States"
}

{
  "first_name": "George",
  "last_name": "Washington",
  "birthday": "1732-02-22",
  "address": {
    "street_address": "3200 Mount Vernon Memorial Highway",
    "city": "Mount Vernon",
    "state": "Virginia",
    "country": "United States"
  }
}

So here I want First Name, Last name, birthday, address all to be csv headers and all the data should be copied under that so that I can have a clean csv file with all the details.


Solution

  • EmEditor will not convert JSON to Excel (or CSV) files by itself. However, you can write a macro to convert JSON to CSV files. If you need support on writing a macro, can you write a small JSON sample, and how you want the output CSV look like?

    Updates

    This is a macro for you to convert JSON to CSV. It allows up to only one-deep nested structure of JSON. This macro checks if a delimiter (comma) exists in data but does NOT check if a double quotation mark or new line code exists. I also noticed your JSON data contains syntax errors, and I corrected it.

    Macro JsonToCsv.jsee :

    function AddStr( s1, s2 )
    {
        if( s2.toString().indexOf( ',' ) != -1 ) {
            s2 = '"' + s2 + '"';
        }
        return s1 + s2;
    }
    
    sHeading = "";
    sBody = "";
    document.selection.SelectAll();
    var json = JSON.parse(document.selection.Text);
    for( property in json ) {
        if( typeof json[property] === 'object' ) {
            for( property2 in json[property] ) {
                if( sHeading.length != 0 ){
                    sHeading += ",";
                    sBody += ",";
                }
                sHeading = AddStr( sHeading, property2 );
                sBody = AddStr( sBody, json[property][property2] );
            }
        }
        else {
            if( sHeading.length != 0 ){
                sHeading += ",";
                sBody += ",";
            }
            sHeading = AddStr( sHeading, property );
            sBody = AddStr( sBody, json[property] );
        }
    }
    editor.NewFile();
    document.selection.Text = sHeading + "\r\n" + sBody + "\r\n";
    editor.ExecuteCommandByID(22528);  // switch to CSV mode
    

    Input:

    {
      "name": "George Washington",
      "birthday": "February 22, 1732",
      "first_name": "George",
      "last_name": "Washington",
      "birthday": "1732-02-22",
      "address": {
        "street_address": "3200, Mount Vernon Memorial Highway",
        "city": "Mount Vernon",
        "state": "Virginia",
        "country": "United States"
      }
    }
    

    Output:

    name,birthday,first_name,last_name,street_address,city,state,country
    George Washington,1732-02-22,George,Washington,"3200, Mount Vernon Memorial Highway",Mount Vernon,Virginia,United States
    

    You can run this macro after you open your data file. To do this, save this code as, for instance, JsonToCsv.jsee, and then select this file from Select... in the Macros menu. Finally, open your data file, and select Run in the Macros menu while your data file is active.