Search code examples
javascriptcsvpapaparse

Japanese CSV to JSON parse Error


I want to parse my CSV file into a JSON file. I have parsed it but it's not getting Japanese characters.

I am using Papa Parser to parse CSV to JSON.

Here is my Code:-

Papa.parse("http://localhost:3000/readdata.csv", {
    download: true,
    header: true, 
    worker: true,
    encoding: 'Shift-JIS',
      console.log(row);
    },
    complete: function() {
      console.log("All done!");
    }
});

answer:-

{��s����: "0", ��s��(��): "�����", ��s��(����): "���{��s", �x�X����: "79", �x�X��(��): "���-", …}

parsing works but not working encoding.

Is there any other solution to parse Japanese CSV (huge file) to JSON?


Solution

  • I didn't really modify the relevant parts of your code, but seems to work for me. Firefox 58 here.

    <html>
    <head>
        <script src="papaparse.js"></script>
    </head>
    <body>
        <script>
        function openFile(event) {
            var input = event.target;
            Papa.parse(input.files[0], {
                download: true,
                header: true, 
                worker: true,
                encoding: 'Shift-JIS',
                complete: function(results) {
                    console.log("All done!", results.data);
                }
            });
        }
        </script>
        <input type='file' onchange='openFile(event)'><br>
    </body>
    </html>
    

    Unfortunately, this didn't work for me when I retrieved the file from a URL, even if I set the web servers headers to:

    Content-Type: text/plain; charset=shift_jis
    

    or

    Content-Type: text/plain; charset=shift-jis
    

    Update: Actually, this appears to work just fine. You may run into problems if you've got an old version in the browser cache however.

    Here's a demo: https://blog.qiqitori.com/stackexchange/papaparse/papaparse-sjis-from-url.html

    $ curl -I https://blog.qiqitori.com/stackexchange/papaparse/readdata-charset-sjis.csv
    HTTP/1.1 200 OK
    Date: Thu, 22 Mar 2018 05:23:49 GMT
    Server: Apache/2.4.25 (Debian)
    Last-Modified: Wed, 21 Mar 2018 15:48:17 GMT
    ETag: "15a-567ee1ea9847f"
    Accept-Ranges: bytes
    Content-Length: 346
    Vary: Accept-Encoding
    Content-Type: text/plain; charset=shift_jis
    

    If you cannot change your server settings, here's a work-around that will allow you to do this without changing the server settings at all: I suggest using XMLHttpRequest to load the CSV into a variable, and forcing the encoding to Shift-JIS.

    function load(url, callback) {
        var Xhr = new XMLHttpRequest();
        Xhr.onreadystatechange = function () {
            if (Xhr.readyState === 4 && Xhr.status === 200)
                callback(Xhr.responseText);
        };
        Xhr.open("GET", url, true);
        Xhr.overrideMimeType('text/plain; charset=Shift_JIS');
        Xhr.send();
    }
    
    load("http://.../readdata.csv", function (contents) {
            Papa.parse(contents, {
    //          download: true,
                header: true, 
                worker: true,
    //          encoding: 'Shift-JIS',
                complete: function(results) {
                    console.log("All done!", results.data);
                }
            });
        });