1

I'm having some trouble with my JsonBuilder. I'd like the output to look like the following:

{
    "appointmentCheckResult": [
        {
            "itexxmCode": "98765432",
            " needAppointmentCheckFlag ": "Y"
        },
        {
            "itemCode": "98765433",
            "needAppointmentCheckFlag": "N"
        }
    ]
}

what I get is :

{
    "appointmentCheckResult": {
        "xxx": [
            {
                "itemCode": "12345",
                "needAppointmentCheckFlag": "Y"
            },
            {
                "itemCode": "5678902",
                "needAppointmentCheckFlag": "Y"
            }
        ]
    }
}

The code is shown below:

import groovy.json.*

def json = new JsonBuilder()
def itemCode = ['12345', '5678902']
def needFlag = 'Y'
json.appointmentCheckResult{xxx([itemCode].transpose().collect {[itemCode:it[0], needAppointmentCheckFlag:needFlag]})}

println JsonOutput.prettyPrint(json.toString())

How can I get rid of the XXX and the "{" which in front of XXX?

Jimi
  • 539
  • 5
  • 21
Yang
  • 11
  • 1

1 Answers1

0

No idea how you're expecting to get the Y and N in the output, or itexxmCode as a key... But assuming they're typos in the expected output, you need something like:

json {
    appointmentCheckResult(
        [itemCode].transpose().collect {
            [itemCode: it[0], needAppointmentCheckFlag: needFlag]
        }
    )
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338