2

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);
});
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
zero298
  • 25,467
  • 10
  • 75
  • 100
  • 3
    No. Why would you want to do that? It's like asking "How do I parse and run a JavaScript program even if it contains syntax errors?" You can't expect a JSON parser to parse something that is not valid JSON. – Vivin Paliath Oct 27 '15 at 23:52
  • You would have to parse the `data` as 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? – Shan Robertson Oct 27 '15 at 23:56
  • 2
    If it works as an object literal, you could always use `eval`, eg `eval('var jsonData = ' + data)` – Phil Oct 27 '15 at 23:56
  • Never use eval on data whose origin you don't know. e.g. a string you got from some anonymous person on the web. If the string contains javascript code it will be executed. – Tesseract Oct 28 '15 at 00:03
  • @VivinPaliath Well, every now and then I get handed a lot of poorly formatted data that I'd like to use without a lot of headache. What prompted me was trying to help [this question](http://stackoverflow.com/q/33380275/691711). – zero298 Oct 28 '15 at 00:08
  • You could try http://hjson.org/. Or just write your own JSON parser. It's not that difficult. JSON is a very simple language. – Tesseract Oct 28 '15 at 00:14
  • If your application contract specifies that you accept JSON then you shouldn't accept anything that isn't. You should return an error message. Using hackish workarounds to get around a legitimate error is absolutely the wrong thing to do. – Vivin Paliath Oct 28 '15 at 00:37

2 Answers2

1

As Vinin Paliath already said, this isn't possible. If incorrect syntax would parse, then it wouldn't be incorrect syntax, would it?

At best, you can try to write a custom function that cleans up common JSON markup mistakes. E.g. that checks for missing quotes like in your examples.

If your JSON is incorrect JSON but would be correct as a javascript object literal, you could use eval. eval('var jsonData = ' + data); I'd highly advise against this with user-entered data because security concerns, though. e.g. someone putting {};alert("intrusive message"); in data.

Wingblade
  • 9,585
  • 10
  • 35
  • 48
  • Thanks, this will work whenever I don't mind using `eval`. I posted an alternative using `new Function()`. – zero298 Oct 28 '15 at 00:17
  • declaring vars in eval is forbidden in strict mode, just return the data, no need to assign it... – dandavis Oct 28 '15 at 01:36
0

Wingblade's answer covers how to do this using eval, which won't run under strict mode. One alternative is to create a Function object with the poorly formatted data as the function body:

var f = new Function("return " + data);
var ff = f();
console.log(ff);

This runs under strict mode, but I'm not sure how far away it is from eval.

Community
  • 1
  • 1
zero298
  • 25,467
  • 10
  • 75
  • 100