1

I wanted to create a string in groovy in following structure

"{\"Changes\": [{\"Action\": \"UPSERT\", \"ResourceRecordSet\": 
       { \"Name\": \"rms-collector-demo.cnqr.delivery.\",\"Type\": 
         \"CNAME\",\"TTL\": 300,\"ResourceRecords\": [{ \"Value\": 
         \"d-4kushcom5y13.execute-api.us-west-2.amazonaws.com\"}]}}]}"

tried with escaping characters but was not useful. can some one help what should be the format used in groovy to define this string.

1
  • You can use multiline string enclosed between either ''' or """ (three quotes). What is your use case? Commented Apr 26, 2018 at 8:50

2 Answers 2

1

Groovy supports multi-line strings, so you can simply use

"""
   {"Changes": [{"Action": "UPSERT", "ResourceRecordSet": 
   { "Name": "rms-collector-demo.cnqr.delivery.","Type": 
     "CNAME","TTL": 300,"ResourceRecords": [{ "Value": 
     "d-4kushcom5y13.execute-api.us-west-2.amazonaws.com"}]}}]}
"""

You can also use ''' to create a normal String.

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of carefully crafting some JSON as a string (and maybe add errors in the process), you can use JsonOutput to create the JSON from a simple data structure:

def json = groovy.json.JsonOutput.toJson([Changes: ["X"]])
println json
// => {"Changes":["X"]}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.