skip to Main Content

I have been all over google and SO trying to figure out how I can pretty print JSON to a file using groovy without unicode characters getting escaped.

String json = JsonOutput.toJson([name: 'Johan Söder', age: 42])
String prettyJson = JsonOutput.prettyPrint(json)
File newFile = new File("data.json")
newFile.write(prettyJson)

Which results in the file content looking like:

{
    "name": "Johan Su00f6der",
    "age": 42
}

Using JsonGenerator and the option disableUnicodeEscaping works, but JsonGenerator does not have a pretty print option so it looks like this:

{"name":"Johan Söder","age":42}

Which is fine for this small example, but my real output isn’t this small and needs to be pretty printed.

Passing the result of JsonGenerator to JsonOutput.prettyPrint reintroduces the unicode escaped characters. Passing the pretty print from JsonOutput to the JsonGenerator creates a whole mess:

"{n    "name": "Johan S\u00f6der",n    "age": 42n}"

With unicode escped characters and not even pretty printed.

I have also tried using JsonBuilder with a JsonGenerator:

JsonGenerator generator = new JsonGenerator.Options().disableUnicodeEscaping().build()
String prettyString = new JsonBuilder([name: 'Johan Söder', age: 42], generator).toPrettyString()
File newFile = new File("data.json")
newFile.write(prettyString)

But the result is still the same:

{
    "name": "Johan Su00f6der",
    "age": 42
}

So now I post this question in the hopes that someone has the solution to be able to pretty print to a file without unicode escaped characters. I would like a Groovy solution without additional libraries.

2

Answers


  1. Using StringEscapeUtils.unescapeJavaScript seems to do the trick:

    import groovy.json.*
    
    String json = JsonOutput.toJson([name: 'Johan Söder', age: 42])
    String prettyJson = StringEscapeUtils.unescapeJavaScript(
        JsonOutput.prettyPrint(json)
    )
    File newFile = new File("data.json")
    newFile.write(prettyJson)
    
    Login or Signup to reply.
  2. I’m suggesting to unescape only unicode characters

    import groovy.json.*
    
    String json = JsonOutput.toJson([name: 'Johan " Söder', age: 42]) // added doublequote for test
    String prettyJson = JsonOutput.prettyPrint(json)
    prettyJson = prettyJson.replaceAll(/\u([0-9a-fA-F]{4})/){Integer.parseInt(it[1], 16) as char}
    File newFile = new File("data.json")
    newFile.write(prettyJson)
    

    result:

    {
        "name": "Johan " Söder",
        "age": 42
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search