First Last
First Last

Reputation: 1

Parsing fails in a big JSON

I have a big JSON file with ~220k records with a size of 223MB. I can open the JSON with a program called Huge JSON Viewew, which means that the file is well structured, but when it comes to parsing it shows a error.


const fs = require("fs");

const rawdata = fs.readFileSync("jsonFile.json");
let inproceedings = JSON.parse(rawdata);

The error it shows:

undefined:1
��[
^

SyntaxError: Unexpected token � in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.<anonymous> (C:\Users\HP\Desktop\DATA\script\jsonFile.js:6:26)
?[90m    at Module._compile (internal/modules/cjs/loader.js:936:30)?[39m
?[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)?[39m
?[90m    at Module.load (internal/modules/cjs/loader.js:790:32)?[39m
?[90m    at Function.Module._load (internal/modules/cjs/loader.js:703:12)?[39m
?[90m    at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)?[39m
?[90m    at internal/main/run_main_module.js:17:11?[39m
PS C:\Users\HP\Desktop\DATA\script>

Upvotes: 0

Views: 667

Answers (1)

Dhananjai Pai
Dhananjai Pai

Reputation: 6005

The problem is that when you readFile it reads as a buffer. use utf-8 encoding when reading [ https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options ]

const rawdata = fs.readFileSync("jsonFile.json",{encoding: 'utf-8'});
let inproceedings = JSON.parse(rawdata);

Upvotes: 1

Related Questions