Create json for rest API

Hello, I am trying to create a json body for a rest API call, but it returned error  

Body as map output cannot be set. Response content type is not json compliant(application/problem+json; charset=utf-8).

I used import groovy.json.JsonBuilder, and created the json as some examples in Q&A, but it doesn't work

def builder=new JsonBuilder()
def line=builder {
    email {
        sender mail.sender
        receiver mail.receiver
        exception mail.exceptiondemand
        subject "test subject"
        exceptionEndDate new Date(System.currentTimeMillis())+1
    }
}
return line

mail is created as bdm object dedicated to API call 

exception is my object created in process and correctly filled

Is anything wrong with my code?
Thanks

Hi,

You can use the the JsonSluper to parse the text response like this:

def body = new JsonSlurper().parseText(bodyAsString); // body is Map (or List of Map) represntation of the JSON response

To create a Business Object from this you must do the mapping by hand

def body = new JsonSlurper().parseText(bodyAsString); def email = new Email() // assumption is that Email is a Business Object in your BDM email.sender = body .sender email.receiver = body.receiver ...

If there is a one-to-one mapping between the response and the business object you can try to use the as keyworg in Groovy.

def body = new JsonSlurper().parseText(bodyAsString); return body as Email // assumption is that Email is a Business Object in your BDM

HTH
Romain

Hello,

Thanks for the response, sorry for the delay I was on another project, so I wasn't clear in my first message the API is a POST call so I need to send the Bonita business object to the API as formated JSON, so I need to create a business Object Email in my BDM, or can I create only the JSON?

here is my workflow:
We create a demand inside Bonita, and we need to send it to this API, who will create a templated Email and send it to specific users to validate or refuse this demand

And we use Bonita Community .
Thanks