1

I have a string which is 'almost' a json string, only that its keys are not surrounded by quotes. Normally it is used by the UI and javascript which does not have a problem in interpreting it. However it seems the JSON parsers in Java that i know of require key to be surrounded by quotes.

Is there a way i can convert the string to a valid json string, probably by using a regular expression in Java. Or is there a JSON lib which is a bit lenient.

The String will be of the form

{
   A : "Val1", 
   B : [ SOME NESTED STUFF], 
   C : "Val3"
}

and i need to convert it to

{
   "A" : "Val1", 
   "B" : [ SOME NESTED STUFF], 
   "C" : "Val3"
}

without affecting any of the nested stuff. The number of keys ie A, B, C is fixed.

Thanks

P.S. I cannot get the appropriate JSON string returned to me, it is a pre existing code and it is quite risky to change it.

3
  • Are Val1 and Val3 guaranteed to contain no double quotes? Commented Feb 7, 2012 at 23:30
  • 1
    ([^"]\\w+[^"]?): ... that's just off the top of my head, but should point you in the right direction. The Idea is to work back from the colon since you know you're looking for one or more word characters that aren't quoted preceding a colon. Commented Feb 7, 2012 at 23:38
  • Thank you all...I started using json-smart which is lenient in parsing the json, and does not require the key to have quotes. code.google.com/p/json-smart Commented Feb 8, 2012 at 20:06

4 Answers 4

1

If you happen to use Jackson it has support for non-standard JSON including unquoted keys: http://www.cowtowncoder.com/blog/archives/2009/08/entry_310.html

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

1 Comment

Thanks....i also realized that Json-smart also has it. code.google.com/p/json-smart
1

If you are sure that all line starting with     unquoted_word : need the word to be quoted, you could use something like:

str.replaceAll("(?m)^(\s+)(\w+)(\s*:)", "$1\"$2\"$3");


But if you can you are probably better off using a proper parser like other answers suggest.

Comments

0

If you really want a regex, this might work:

jsonString.replaceAll("(\\w+)\\s*\\:","\"$1\" :");

That said, if you are really worried about touching the nested stuff and corner cases, you want a real parser, not a regex. There's no way that a regex will be precise enough to avoid messing up if one of your values is the string " A : ". If pingw33n is correct about the jackson parser, it is by far the best answer.

Comments

0

use the following line code to enclose all the keys (single caps letter is considered a valid key here) with double quotes:

str = str.replaceAll("\s([A-Z])\s\:\s\"", "\"$1\" : ");

10 Comments

Breaks on { A : " B : C "}.
if you update like this than it will work s.replaceAll("\\s([A-Z])\\s+([\\:\"])", "\"$1\" $2")
I wasn't even thinking about the unescaped backslashes. Even once they're fixed, the expression will still mess up the second a string contains " (some letter) :".
@cHao: indeed... i did not account for that... i edited my answer to fix this issue by adding the \" at the end of the regex string
@BigFatBaby: With proper JSON and a JSON parser, as the gods intended. Or JS and eval. But either one far exceeds the power of a regular expression. Regexes are quite limited in their ability; you go that route, before too long you'll be tearing your hair out in frustration.
|

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.