1

I am triying to send a callout using JSONGenerator in salesforce but I am having difficuty in putting the things through using .write methods. Any help will be great.

{
    "request" : {
        "authAccount" : {
            "email"     : "email.com",
            "password"  : "test",
            "partnerID" : "test",
            "accountID" : "test"
        },
        "requestList" : [
            {
                "setBridgeUserID" : {
                    "conferenceID" : "test",
                    "userID" : "test"
                }
             },
              {
                "setBridgeUserID" : {
                    "conferenceID" : "test",
                    "userID" : "test"
                }
            }
        ]
    }
}
4

JSONGenerator is much harder to use than to simply serialize Apex objects using JSON.serialize. You can create the small Apex classes manually or use json2apex which outputs the below code for your JSON. (You can then edit the code as required e.g. delete or move the test method, change the outer class name.)

As you can see, creating these Apex classes makes parsing convenient too.

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

    public class AuthAccount {
        public String email;
        public String password;
        public String partnerID;
        public String accountID;
    }

    public class RequestList {
        public SetBridgeUserID setBridgeUserID;
    }

    public Request request;

    public class Request {
        public AuthAccount authAccount;
        public List<RequestList> requestList;
    }

    public class SetBridgeUserID {
        public String conferenceID;
        public String userID;
    }


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }

    static testMethod void testParse() {
        String json = '{'+
        '    \"request\" : {'+
        '        \"authAccount\" : {'+
        '            \"email\"     : \"email.com\",'+
        '            \"password\"  : \"test\",'+
        '            \"partnerID\" : \"test\",'+
        '            \"accountID\" : \"test\"'+
        '        },'+
        '        \"requestList\" : ['+
        '            {'+
        '                \"setBridgeUserID\" : {'+
        '                    \"conferenceID\" : \"test\",'+
        '                    \"userID\" : \"test\"'+
        '                }'+
        '             },'+
        '              {'+
        '                \"setBridgeUserID\" : {'+
        '                    \"conferenceID\" : \"test\",'+
        '                    \"userID\" : \"test\"'+
        '                }'+
        '            }'+
        '        ]'+
        '    }'+
        '}';
        JSON2Apex obj = parse(json);
        System.assert(obj != null);
    }
}

To create the JSON you would create the graph of objects and then serialize:

JSON2Apex.AuthAccount authAccount = new JSON2Apex.AuthAccount();
authAccount.email = 'email.com';
...

JSON2Apex.Request request = new JSON2Apex.Request();
request.authAccount = authAccount;

JSON2Apex root = new JSON2Apex();
root.request = request;

String jsonString = JSON.serialize(root);
| improve this answer | |
  • Hi Kieth,I am expecting something like thisJSONGenerator gen = JSON.createGenerator(true); gen.writeStartObject(); gen.writeStringField('username', 'StringValue'); gen.writeStartObject(); gen.writeObjectField('recipients', r); gen.writeEndObject(); gen.writeEndObject(); System.debug ('>> Piyush' + gen.getAsString()); return gen.getAsString(); – Piyush Singhal Jun 10 '15 at 11:37
  • @PiyushSinghal Yes you could use the JSONGenerator but that requires you to make several method calls per field and to make the method calls in a way that matches the structure of the JSON; it is time consuming to write that code and easy to make mistakes. I'm suggesting instead that you take a different approach where you leave more of the work to the platform and tools. – Keith C Jun 10 '15 at 11:40
  • @SalesForceDoctor well I think I will do with the a simple string as it bit simple and my integration does not require many fields to vary :D – Piyush Singhal Jun 10 '15 at 11:56

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.