Anuruddha
Anuruddha

Reputation: 3245

using '@' in groovy JsonBuilder

I'm trying to construct the following json in groovy.

 {  
   "trace_id":123,
   "@timestamp":"455754534538",
   "body" : "abcd"
 }

Following is the code snippets I used to try this.

import groovy.json.JsonSlurper 
import groovy.json.JsonBuilder
def xmlPayload =  "payload";
def traceId = mc.getProperty('TraceIdProp');
def timeStamp = mc.getProperty('TimestampProp');
builder = new JsonBuilder()

def root = builder trace_Id: traceId, @timestamp: timeStamp, @version: 1, body: xmlPayload

However it seems I cannot use '@' character with the json builder, it's giving the following error.

groovy: 8: expecting an identifier, found '@' @ line 8, column 51.
    = builder trace_Id: traceId, @timestamp

Is this achievable in groovy?

Upvotes: 0

Views: 112

Answers (1)

Jérémie B
Jérémie B

Reputation: 11022

In Groovy, a @field has a special meaning. You should use a quote to use this character :

def root = builder trace_Id: traceId, '@timestamp': timeStamp, '@version': 1, body: xmlPayload

Upvotes: 2

Related Questions