1

Hello I am trying to create a list of json objects in groovy

List relClinicStatementList = []
            for (BloodTestRow row in BTList){
                def jsonListBuilder = new groovy.json.JsonBuilder()
                def internalJson = jsonListBuilder{
                        'targetRelationshipToSource' {
                            'code' 'part-of'
                            'codeSystem' 'MG'
                        }
                        'observationResult'{
                            'observationFocus'{
                                'code' "${row.exam}"
                                'codeSystem' 'mobiguide'
                                'displayName' "${row.exam}"
                            }
                            'observationValue' {
                                'physicalQuantity' {
                                    'value' "${row.value}"
                                    'unit' "${row.unit}"
                                    }
                            }
                        }
                    }
                println jsonListBuilder.toPrettyString()
                relClinicStatementList.add(internalJson)

            }

And the toPrettyString() method correctly shows the json structure I want. However if at the end of the loop I try to print all of the items I have in the list like this:

                    for (JsonBuilder entry in relClinicStatementList){
                    println entry.toPrettyString()
                    }

I get all the elements inside my relClinicalStatement list to be equal to the latest I created... I felt like declaring a new JsonBuilder at each loop would prevent this behaviour... am I missing something? I must admit I come from Java and have the feeling that using groovy classes here makes this behave a little differently from what I expect. How do I solve this issue?

Thanks in advance

neneItaly
  • 263
  • 2
  • 9
  • shouldn't you add jsonListBuilder to relClinicStatementList instead of internalJson? – cfrick Feb 25 '14 at 15:54
  • @cfrick, I tried both solutions (adding internalJson vs. adding jsonListBuilder) but this didn't solve the problem. Still the list has all objects equal to the last json I create. – neneItaly Feb 26 '14 at 08:19

1 Answers1

1

I can't reproduce the behaviour you are seeing, but I think the problem is that I don't believe internalJson is what you think it is (it's a list of 2 closures).

If you change your code to:

List relClinicStatementList = btList.collect { row ->
    new groovy.json.JsonBuilder( {
        targetRelationshipToSource {
            code       'part-of'
            codeSystem 'MG'
        }
        observationResult {
            observationFocus {
                code        "$row.exam"
                codeSystem  'mobiguide'
                displayName "$row.exam"
            }
            observationValue {
                physicalQuantity {
                    value "$row.value"
                    unit  "$row.unit"
                }
            }
        }
    } )
}

relClinicStatementList.each { entry ->
    println entry.toPrettyString()
}

Does it work as you'd expect?

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Thanks tim. What you higlited was exactly the point. My way of creating the list indeed created a list of closures (which is not what I wanted). I still have to catch the details on grails and some of its objects and structures (there is no such thing as a closure in Java :) ) but this definetly helped! Thanks – neneItaly Feb 26 '14 at 08:15