admin管理员组

文章数量:1327808

I am being sent an ill formed JSON string from a third party. I tried using JSON.parse(str) to parse it into a JavaScript object but it of course failed.

The reason being is that the keys are not strings:

{min: 100}

As opposed to valid JSON string (which parses just fine):

{"min": 100}

I need to accept the ill formed string for now. I imagine forgetting to properly quote keys is a mon mistake. Is there a good way to change this to a valid JSON string so that I can parse it? For now I may have to parse character by character and try and form an object, which sounds awful.

Ideas?

I am being sent an ill formed JSON string from a third party. I tried using JSON.parse(str) to parse it into a JavaScript object but it of course failed.

The reason being is that the keys are not strings:

{min: 100}

As opposed to valid JSON string (which parses just fine):

{"min": 100}

I need to accept the ill formed string for now. I imagine forgetting to properly quote keys is a mon mistake. Is there a good way to change this to a valid JSON string so that I can parse it? For now I may have to parse character by character and try and form an object, which sounds awful.

Ideas?

Share Improve this question edited Nov 9, 2015 at 2:43 Mogsdad 45.8k21 gold badges162 silver badges285 bronze badges asked Apr 19, 2013 at 17:56 lostintranslationlostintranslation 24.6k53 gold badges172 silver badges288 bronze badges 9
  • It looks like there are some solutions here: stackoverflow./questions/4210160/… – showdev Commented Apr 19, 2013 at 17:59
  • I think using a Function constructor should work in this case : var data = new Function('return '+illFormedJSON;)() – m90 Commented Apr 19, 2013 at 18:00
  • 1 (?:[a-z]+):+ I'd say search for all non proper keys. This is not a plete regex for should give you a place to start. Don't have time to fully answer the question. – travis Commented Apr 19, 2013 at 18:02
  • @m90 Isn't that eval in disguise? How about a JSON like: {a: alert('whoa!')} – UltraInstinct Commented Apr 19, 2013 at 18:13
  • 1 Wow thanks for the insight Kolink. Ever write code that doesn't pile.....its not really code. Didn't really think the title, 'Cannot parse not JSON into JS object would get many hits.' You know what I meant. – lostintranslation Commented Apr 19, 2013 at 21:19
 |  Show 4 more ments

3 Answers 3

Reset to default 5

You could just eval, but that would be bad security practice if you don't trust the source. Better solution would be to either modify the string manually to quote the keys or use a tool someone else has written that does this for you (check out https://github./daepark/JSOL written by daepark).

I did this just recently, using Uglifyjs to evaluate:

var jsp = require("uglify-js").parser;
var pro = require("uglify-js").uglify;

var orig_code = "var myobject = " + badJSONobject;
var ast = jsp.parse(orig_code); // parse code and get the initial AST
var final_code = pro.gen_code(ast); // regenerate code

$('head').append('<script>' + final_code + '; console.log(JSON.stringify(myobject));</script>');

This is really sloppy in a way, and has all the same problems as an eval() based solution, but if you just need to parse/reformat the data one time, then the above should get you a clean JSON copy of the JS object.

Depending on what else is in the JSON, you could simply do a string replace and replace '{' with '{"' and ':' with '":'.

本文标签: javascriptParse ill formed JSON stringStack Overflow