William Pham
William Pham

Reputation: 291

Ambiguous expression could be either a parameterless closure expression or an isolated open code block;

I'm having the following code block which will create an object with is concatenated string of all objects within "basixCertificateNumbers" array.

def object= jsonSlurper.parseText '''
{
    "basixCertificateNumbers": [
        {
          "basixCertificateNumber": "012-012"
        },
        {
            "basixCertificateNumber": "045-123"
          }
      ]
}
'''
def concatdObj = jsonSlurper.parseText '''
{
  "basixNumber" : ""
}
'''

def content = object.each{ entry->
    if(entry.value.getClass().name === "java.util.ArrayList"){
        for (basixIndex = 0 ; basixIndex < entry.value.size(); basixIndex++){
            entry.value[basixIndex].each{ nestedEntry->{
                concatdObj.basixNumber = concatdObj.basixNumber + nestedEntry.value + " "
            }}
        }
        concatdObj.basixNumber = concatdObj.basixNumber.substring(0, concatdObj.basixNumber.length() - 1);
    }}

I'm currently receiving the following errors:

 Ambiguous expression could be either a parameterless closure expression or an isolated open code block;
   solution: Add an explicit closure parameter list, e.g. {it -> ...}, or force it to be treated as an open block by giving it a label, e.g. L:{...} @ line 41, column 56.
   asixIndex].each{ nestedEntry->{
                                 ^

Even though the solution suggested is to put the label on it, I'm not sure where would be the optimal way to put it.

The current solution would be remove the "{" after nestedEntry, which will be something like this:

entry.value[basixIndex].each{ nestedEntry->
                concatdObj.basixNumber = concatdObj.basixNumber + nestedEntry.value + " "
            }

However, I believe this is not an optimal way of doing things, so if anyone would have a better idea. It would be a great help!

My desire output would be:

{
   "basixNumber" : "012-012 045-123"
}

Upvotes: 0

Views: 5488

Answers (1)

tim_yates
tim_yates

Reputation: 171154

You can just do

def content = [
    basixNumber: object.basixCertificateNumbers.basixCertificateNumber.join(' ')
]
String jsonOutput = new JsonOutput().toJson(content)

You don't need concatdObj

Upvotes: 2

Related Questions