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);
});
7
  • 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. Commented Oct 27, 2015 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? Commented Oct 27, 2015 at 23:56
  • 2
    If it works as an object literal, you could always use eval, eg eval('var jsonData = ' + data) Commented Oct 27, 2015 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. Commented Oct 28, 2015 at 0: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. Commented Oct 28, 2015 at 0:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this will work whenever I don't mind using eval. I posted an alternative using new Function().
declaring vars in eval is forbidden in strict mode, just return the data, no need to assign it...
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.

1 Comment

eval works fine in strict mode, better in fact, not sure what you're describing...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.