Consider the file below being used as input for the accompanying JavaScript used in node.js. The script will fail with "Unexpected token h" because hello is not in quotes. Is there a way to parse this file anyway without having to fix the string first?
data.json
{
"foo": "bar",
hello: "world",
"jane": "fonda",
jimmy: "buffet"
}
index.js
/*jslint node:true*/
"use strict";
var fs = require("fs");
fs.readFile("./data.json", "utf8", function (err, data) {
var jsonData = JSON.parse(data);
console.log(jsonData);
});
dataas a string and write a series of checks and fixer functions. Like, "does this line start with a character other than a quote? if yes surround it with one". But, if you are loading invalid JSON, there are infinite different situations you would have to test for. The bigger question, is where are you getting invalid JSON from? Why do you have invalid JSON?eval, egeval('var jsonData = ' + data)