Home > Enterprise >  Convert excel to json but with only one header
Convert excel to json but with only one header

Time:01-19

I am trying to write an html with JS program that will convert an excel file into json which is does bit it does not format it the way I need to. So basically it spits out when finished

[
  {
    "imei": "357271093291264"
  },
  {
    "imei": "353094106032150"
  },
  {
    "imei": "353112106434588"
  }
]

but what I need is.

[
{
"imei": "357271093291264", "353094106032150", "353112106434588"
}
]

So it is taking imei from cell A1 and using it over and over. I just need it

to keep adding on as I go down the rows.

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://unpkg.com/[email protected]/bundle/read-excel-file.min.js"></script>
</head>
<body>
    <div style="margin: auto;width: 50;margin-top: 80px;padding: 30px;background-color: #dedede;">
        <h2>Excel to JSON Converter</h2>
        <input type="file" id="input" />
        <br> <br>
        <textarea name="json-data" id="json-data" rows="25" style="width: 100%;"></textarea>
        <br><br>
        <button id="dl-json">Download JSON File</button>
    </div>
<script>
    var input = document.getElementById('input');
    input.addEventListener('change', function(){
        readXlsxFile(input.files[0]).then(function(data){
            var i = 0;
            var headers = [];
            var json_object = [];

            data.map((row, index)=> {
                if (i == 0){
                    headers = row;
                }

                if (i > 0){
                    var temp = {};
                    for (var x = 0; x < row.length; x  ){
                        temp[headers[x]] = row[x];
                    }
                    json_object.push(temp);
                }
                i  ;
            });
            document.getElementById('json-data').value = JSON.stringify(json_object, null, 2)
        });
        document.getElementById('dl-json').onclick = function() {
            var json_str = document.getElementById('json-data').value;
            downloadObjectAsJson(json_str, '');
        }

        function downloadObjectAsJson(str, filename){
            var data_str = "data:text/json;charset=utf-8,"   encodeURIComponent(str);
            var anchor = document.createElement('a');
            anchor.setAttribute("href",  data_str);
            anchor.setAttribute("download", filename   ".json");
        }
    });
</script>
</body>
</html>

I have tried playing around with it and pulling out certain parts and setting different variables to certain values.

CodePudding user response:

The shape of your output doesn't seem to make sense. Do you want the first element in your output array to be a key:value pair such as "headerText":"row2Value", and then the rest just strings?

If so, this should work for you:

var input = document.getElementById("input");
input.addEventListener("change", function () {
    readXlsxFile(input.files[0]).then(function (data) {
        let exportData = [];

        for (i = 1; i < data.length; i  ) {
            i === 1
                ? exportData.push({ imei: data[i].toString() })
                : exportData.push(data[i].toString());
        }

        document.getElementById("json-data").value = JSON.stringify(exportData);
    });

    document.getElementById("dl-json").onclick = function () {
        var json_str = document.getElementById("json-data").value;
        downloadObjectAsJson(json_str, "");
    };

    function downloadObjectAsJson(str, filename) {
        var data_str =
            "data:text/json;charset=utf-8,"   encodeURIComponent(str);
        var anchor = document.createElement("a");
        anchor.setAttribute("href", data_str);
        anchor.setAttribute("download", filename   ".json");
    }
});

If you only need the key, then an array of values, this will work better for you:

readXlsxFile(input.files[0]).then(function (data) {
        let exportData = [];

        for (i = 1; i < data.length; i  ) {
            exportData.push(data[i].toString());
        }

        document.getElementById("json-data").value = JSON.stringify({
            imei: exportData,
        });
    });

CodePudding user response:

Try This

var input = document.getElementById('input');
input.addEventListener('change', function(){
    readXlsxFile(input.files[0]).then(function(data){
        var json_object = [];
        headers = ['imei'];

        data.map((row, index)=> {
            json_object[0] = { imei: row.join(",") };
        });
        document.getElementById('json-data').value = JSON.stringify(json_object, null, 2)
    });
    document.getElementById('dl-json').onclick = function() {
        var json_str = document.getElementById('json-data').value;
        downloadObjectAsJson(json_str, '');
    }

    function downloadObjectAsJson(str, filename){
        var data_str = "data:text/json;charset=utf-8,"   encodeURIComponent(str);
        var anchor = document.createElement('a');
        anchor.setAttribute("href",  data_str);
        anchor.setattribute("download", filename   ".json");
    }
});
  • Related