I have a list of map and list objects. These objects needs to be sent as json content in an http request. The objects are

Map<String,String> leftFileParameters = new Map<String,String>();
    Map<String,String> rightFileParameters = new Map<String,String>();
    Map<String, Object> commonParameters  = new Map<String,Object>();

public void initiateParamas() {


Now, I have a method as shown below. I want to serialize all the objects shown above and serialize it and set it as the body of request.

public String getURL(){
 HttpRequest req = new HttpRequest();
 req.setBody() //cannot serialize here.

So, How do i send all the objects as single json and send it in the http request body. Is there any class for doing that?


This code will do what you require:

req.setBody(JSON.serialize(new Map<String, Object>{
    'left': leftFileParameters,
    'right': rightFileParameters,
    'common': commonParameters

For more information, See the JSON.serialize method documentation. That method can work on maps and so the above code creates a map to hold the other maps. You can use whatever names you like instead of "left" etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.