简体   繁体   中英

Add double quotes to JSON input using groovy script

I'm working with Json file and the json structure is incorrectly formatted. I would like to add double quotes (") in the below JSON using groovy scripting.

Input File:

{"I_BEGDA":2020-01-01,"I_ENDDA":2020-01-21,"I_TOTAL":"X"}

Expected Output:

{"I_BEGDA":"2020-01-01","I_ENDDA":"2020-01-21","I_TOTAL":"X"}

I tried multiple scripts but none of them looks working. Appreciate your help in fixing the same.

Thanks

Try something like this:

import groovy.json.*

def input ='{"I_BEGDA":2020-01-01,"I_ENDDA":2020-01-21,"I_TOTAL":"X"}'
def output = new JsonBuilder(
    new JsonSlurper().with { it.type = JsonParserType.LAX ; it }.parseText(input)
)
println(output)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM