Framing JSON object in groovy - Streamsets

157 Views Asked by At

I am pretty new to Streamsets and I finding it a little confusing and challenging to frame a JSON object inside my Groovy Evaluator object.

I need to frame the below JSON:

{
    "filter": "(equals(type,'my/specific/Type') and equals(attributes.number, '1234') and (equals(attributes.status,'ACTIVE'))",
    "max": 10
}

I have tried this:

import groovy.json.*

records = sdc.records
for (record in records) {
    try {
       event = "{"filter": "(equals(type,'my/specific/Type') and equals(attributes.number, '1234') and (equals(attributes.status,'ACTIVE'))","max": 10}"
       record.value = event

        // Write a record to the processor output
        sdc.output.write(record)
    } catch (e) {
        // Write a record to the error pipeline 
        sdc.log.error(e.toString(), e)
        sdc.error.write(record, e.toString())
    }
}

But I receive the below error:

SCRIPTING_03 - Script failed to compile: 'javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1076.groovy: 6: unexpected token: and @ line 6, column 59. uals(type,'my/specific/Type') and equals ^ 1 error '

Kindly help in resolving this.

1

There are 1 best solutions below

0
Roman On

It's not about JSON per se, but about escaping a double-quote character. Groovy nothing but Java + some syntactic sugar, so escaping a " character in Groovy is the same: use ", for example:

foo = "I use so called \"Groovy\" for that"