javajsonregexstringgson

Put missing double quotes in a JSON string value in java


I have a json string where all the values need to be surrounded with double quotes. for example (just a sample, it contains many similar fields)

    {"Id": "2017",
     "Currency": "AUD",
     "Date": 2020-06-22,
     "InCash": 0.000, 
     "Dep": "ABC90",
     "sumCash": 770.87,
     "AnotherDate": 2020-06-21}

to

     {"Id": "2017",
     "Currency": "AUD",
     "Date": "2020-06-22",
     "startCash": "0.000", 
     "Dep": "ABC90",
     "sumCash": "770.87",
     "AnotherDate": "2020-06-21"}
        

I am trying with regular expressions but its breaking the 'Date' fields.

jsonString.replaceAll(":[ ]*([\\w@\\.]+)", ": \"$1\"")

also tried with gson library, but its only putting the quotes on date values and not on the decimal values.

new JsonParser().parse(jsonString).toString()

What exactly I need to do to achieve it?


Solution

  • Got it working with the below regex expression.

    jsonString.replaceAll(": [ ]*([\\w@\\.-]+)", ": \"$1\""));
    

    Thanks everyone for your help and support !