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.
([^"]\\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.