0

I'm using JsonBuilder to create a new Json object. Additionally I have to do an escaping of control and special charaters.

Therefore I'm using the JsonOutput.toJson which adds an additional "content" element.

sample XML input:

<?xml version="1.0" encoding="utf-8"?>
<TestRequest>
        <Note>test note....ÄäÜü ß /  $ % §  "</Note>
        <Note2>This is another note. here comes the Zeilenumbruch...bruch&#xA;...hier geh's weiter&#xA;</Note2>
</TestRequest>

sample coding:

class Mapping_41_Test_Control_Chars {
static main(args) {
    def in_xml                                      = new XmlSlurper().parse("./test/Sample_Control_Character.xml")
    def jsonBuilder                                 = new JsonBuilder()

    jsonBuilder{
        note in_xml.Note.toString()
        note2 in_xml.Note2.toString()
    }

    println "-------- prettyPrint_JsonOutput.toJson:"
    println JsonOutput.prettyPrint(JsonOutput.toJson(jsonBuilder));


    def json = JsonOutput.toJson([note: 'test note....ÄäÜü ß /  $ % §  "'])

    println  "-------- json:"
    println JsonOutput.prettyPrint(json)
}

JsonOutput.toJson:

-------- prettyPrint_JsonOutput.toJson:
{
    "content": {
        "note": "test note....\u00c4\u00e4\u00dc\u00fc \u00df /  $ % \u00a7  \"",
        "note2": "This is another note. here comes the Zeilenumbruch...bruch\n...hier geh's weiter\n"
    }
}
-------- json:
{
    "note": "John Doe",
    "age": "test note....\u00c4\u00e4\u00dc\u00fc \u00df /  $ % \u00a7  \""
}

Do you know an option to use JsonOutput with JsonBuilder and get the required output eliminating the "content" element? I need to escape the special characters therefore I cannot use toString

Thanks & Regards Marco

Marco
  • 25
  • 2
  • 6

2 Answers2

1

managed to get it solved via the following:

JsonOutput.toJson(jsonSlurper.parseText(jsonBuilder.toString()))

Marco
  • 25
  • 2
  • 6
0

You don't have to use JsonOutput.toJson() when working with JsonBuilder - it has a method JsonBuilder.toPrettyString() that creates a String representation of your JSON object.

import groovy.json.JsonBuilder

def builder = new JsonBuilder()
builder {
    name 'John Doe'
    age 99
}

println builder.toPrettyString()

Output

{
    "name": "John Doe",
    "age": 99
}
Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131
  • Thanks for your response. Sorry, maybe I was not precise enough: I'm generating a Json from an XML input which contains the following tags: test note....ÄäÜü ß / $ % § " This is another note. here comes the Zeilenumbruch...bruch ...hier geh's weiter I would have to escape the required special charaters. This is the reason why I use JsonOutput. – Marco Jan 10 '18 at 09:41
  • 1
    @Marco Please update your question then. Paste all snippets needed to reproduce the issue: part of XML, code snippet where you convert XML to JsonBuilder and how you use JsonOutput. Otherwise we don't have sufficient information to help you finding a solution to your problem. – Szymon Stepniak Jan 10 '18 at 09:49
  • done. put some sample coding as well as sample XML input. – Marco Jan 10 '18 at 11:56