6

I have an invalid json string like following,

"{one: 'one', two: 'two'}"

I tried to use JSON.parse to convert it to an object. however, this is not valid json string. Is there any functions can convert this invalid format into a valid json string or directly convert into an object?

eded
  • 3,778
  • 8
  • 28
  • 42
  • 2
    How about using valid json strings in the first place instead? And also, you may find [JSONLint](http://jsonlint.com/) helpful in validating json strings. – JW Lim Jun 28 '14 at 01:13
  • 1
    Related: [Convert invalid json into valid json](http://stackoverflow.com/q/8815586) and [Regular expression to fix invalid JSON](http://stackoverflow.com/q/10401004). – JW Lim Jun 28 '14 at 01:16
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval – Dr.Molle Jun 28 '14 at 01:17
  • 1
    think of an edge case where you are accepting invalid user input. this is a legitimate question – random-forest-cat Apr 07 '15 at 01:14

2 Answers2

6

IF your example syntax is the same as your real JSON, JSONLint says you need double quote for the name AND the value.

In this case only, use these replace calls:

var jsontemp = yourjson.replace((/([\w]+)(:)/g), "\"$1\"$2");
var correctjson = jsontemp.replace((/'/g), "\"");
//yourjson = "{one: 'one', two: 'two'}"
//jsontemp = "{"one": 'one', "two": 'two'}"
//correctjson = "{"one": "one", "two": "two"}"

However you should try to work with a valid Json in the first place.

Crow EH
  • 86
  • 5
  • @eded Take into account that this regular expression will also convert strings like `"{one: 'one tree:', two: 'two'}"` to `"{"one": "one "tree":", "two": "two"}"` (it will match any word followed by ":", even if it is part of the value, not the key), but I think replacing the string using a regular expression is the best way to go in this case. As @Crow EH said, the risk-free/ideal way would be to have a valid JSON string in the first place. – Aylen Jun 28 '14 at 01:57
  • It'd also have problems with keys containing spaces or punctuation, and keys containing colons would be a further complication. It would be hard to handle those cases with just regular expressions, especially if nested objects are a possibility; you might need a more complicated algorithm combining regexes with split(). It's hard to say without knowing more about where the problem JSON is coming from and what it might contain. – bobtato Jun 28 '14 at 03:13
1

If the question is "can I convert invalid JSON into valid JSON", in the general case the answer is obviously "no"; where would you even start with a string like "$@!~~"?

In this particular case, the JSON is only invalid because the property names are not quoted; as JavaScript, the string is valid and could be parsed using, for example,

var myObj = eval( "x=" + myString );

or better

var myObj = (new Function("return " + myString))();

However, this is potentially very unsafe and you should not do it unless you are positive the string cannot cause harm (which seems unlikely if you aren't in a position to generate valid JSON in the first place). It also won't help you if the JSON code is invalid in other ways, and it will fail if the property names are not valid JS identifiers.

For a proper answer it would be useful to know more about the context of this question.

bobtato
  • 1,157
  • 1
  • 9
  • 11