JSFiddle - React, Tailwind, and code Playground

by evan

HTML

<p id="heading">JSON to CSV Converter for Google Location History</p>
<p>Get your Location History from <a href="https://www.google.com/settings/takeout/custom">Google Takeout</a> and locate the "<strong>LocationHistory.json</strong>" file from the download bundle. Open it with a text editor and paste its contents into the first textbox. (If you have a lot of history, it will take several seconds to paste.) Then, click the "Convert to CSV" button and copy the output into a new text file. You may upload this output to <a href="http://www.google.com/drive/apps.html#fusiontables">Fusion Tables</a> or for other purposes.

<hr />
    <p>Paste Your JSON Here:</p>
    <textarea id="json" class="text"></textarea>
    <br />
    <button id="convert">Convert to CSV</button>
    &nbsp;&nbsp;
        <button id="download">Download CSV</button> (May crash your browser- I recommend just copy/pasting)
    <textarea id="csv" class="text"></textarea>
<p>Based on code posted <a href="http://stackoverflow.com/a/4130939/317" target="_blank">here on StackOverflow</a> and <a href="http://jsfiddle.net/sturtevant/vUnF9/">this great Fiddle</a></p>

CSS

#heading { font-size: x-large; font-weight: bold; }
.text { width: 99%; height: 200px; }
.small { font-size: small; }

JavaScript

function JSON2CSV(objArray) {
    var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
    
    array = array.locations;

    var str = '';
    var line = '';

    var head = array[0];
    for (var index in array[0]) {
        line += index + ',';
    }
    
    line = line.slice(0, -1);
    str += line + '\r\n';

    for (var i = 0; i < array.length; i++) {
        var line = '';

        for (var index in array[i]) {
            var value = array[i][index];
            
            // The values I was getting for lat/long were multiplied by 10,000,000
            if (index.indexOf('latitude') === 0 || index.indexOf('longitude') === 0) {
                value = value / 10000000;
            }
            
            // get timestamp in seconds, not milliseconds
            if (index === 'timestampMs') {
                value = Math.floor(parseInt(value, 10) / 1000);
            }
            
            value += "";
            line += '"' + value.replace(/"/g, '""') + '",';
        }

        line = line.slice(0, -1);
        str += line + '\r\n';
    }
    return str;
    
}
        
$("#convert").click(function() {
    var json = $.parseJSON($("#json").val());
    var csv = JSON2CSV(json);
    $("#csv").val(csv);
});
    
$("#download").click(function() {
    var json = $.parseJSON($("#json").val());
    var csv = JSON2CSV(json);
    window.open("data:text/csv;charset=utf-8," + escape(csv))
});