I am learning Integration from this Trailhead

Pre-existing JSON at the endpoint

{"animals":["majestic badger","fluffy bunny","scary bear","chicken"]}

Code that i have written to add three more elements to animals

        HTTP container = new HTTP();
        HttpRequest request = new HttpRequest();
        request.setBody('{"name": ["mighty moose","mighty","moose"]}');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        HttpResponse response = container.send(request);
        if(response.getStatusCode() == 201){

By executing the above code I'm getting the following response that is that the whole value ["mighty moose","mighty","moose"] is getting inserted as one value

{"animals":["majestic badger","fluffy bunny","scary bear","chicken",["mighty moose","mighty","moose"]]}

But i want the output to be that each value to get inserted as three different values in a single web callout

{"animals":["majestic badger","fluffy bunny","scary bear","chicken","mighty moose","mighty","moose"]}

If I change the body as

request.setBody('{"name": "mighty moose","name":"mighty","name":"moose"}');

Only the last one is getting added rather than all 3

{"animals":["majestic badger","fluffy bunny","scary bear","chicken","moose"]}
  • Is the endpoint a salesforce rest resource that you control? If not then not sure we can help much without details on the endpoint. If it is, posting that code would help provide clarity otherwise all we have is a question with unrelated working code. How to Ask – Eric May 9 '17 at 14:31
  • @Eric Actually I am learning Integration from Trailhead trailhead.salesforce.com/modules/apex_integration_services/… So i was hitting the server that they have given which is on heroku platform So it had the example to insert 1 value so was trying to add in multiple values like insert an array of objects rather than one – david raj May 9 '17 at 14:34
  • 2
    I added that to your question. In future add clarity in the question itself not in the comments – Eric May 9 '17 at 15:15

The answer would be if the remote service was able to process it would be to post the object array.

[{"name": "mighty moose"},{"name": "dumb moose"}]

results in a response of

USER_DEBUG|[14]|DEBUG|{"animals":["majestic badger","fluffy bunny","scary bear","chicken",null]}

The problem is that the service is only set up to accept a single value not an array so you will not be able to do it given the current state of the external service

{"name":"mighty moose", "name":"dull"}

Is not valid JSON as there are duplicate keys. The service does process it however and stores the last value dull in the returned results

Attempts at multiple calls will overwrite your previous addition. It appears from my tests at least that you can add one and only one to the list.

Remember, the trailhead is setup to perform a specific task. Anything outside of what is explicitly outlined may not work

| improve this answer | |

Earlier I have answered this question which will help you to understand how JSON stores the Array of objects. In that example,

  • Parent is Account
  • Child is Opportunity
  • Grand Child is Task

I have a Parent->Child->GrandChild .. I need one object constructed and returned for a rest call

rest testing

So you need to prepare multiple records like this:

[{"name": "mighty moose"},{"name": "dumb moose"}]

Though Eric has correctly mentioned in this answer that "The problem is that the service is only set up to accept a single value not an array so you will not be able to do it given the current state of the external service"

| improve this answer | |
  • This does not work in the context of the question. The JSON is valid but the service is not set up to handle it – Eric May 9 '17 at 15:10
  • @Eric That's true, you have already given that answer – Santanu Boral May 9 '17 at 15:11
  • Yup. It could be made highly useful if you detail how a JSON Array of objects is constructed focusing on the OP question rather than details that are out of context. Otherwise it is just repeating what has already been said... – Eric May 9 '17 at 15:19

Try to escape the quotes: req.setBody('{\"name:\" \"value\"}');

| improve this answer | |
  • But how to do for multiple values you have just given for one – david raj May 9 '17 at 14:41
  • Did it work? Why are people down voting? I notice from the post above that you have a duplicated key as well, so it would be something like: req.setBody(JSON.serialize('{"name": "mighty moose","name":"mighty"}')) – Dudu Nato May 9 '17 at 14:59
  • @DuduNato - Because Your answer is not correct as it is a single value and your JSON in the comment is not valid as You have duplicate keys (name). Seems you did not do your research before posting the answer yet you chastise the OP for the same thing – Eric May 9 '17 at 15:12
  • 2
    @DuduNato - The JSON will work just fine without serializing it. In fact, serializing it will cause a 400 response. Appreciate your participation so please do continue. A bit of research into answers will benefit everyone. Downvotes are not meant to be punitive, rather they are meant to make you and others aware that the solution provided may not be complete or valid. Please take it as an opportunity to expand your knowledge – Eric May 9 '17 at 15:39
  • 1
    @Eric interesting! Good to know! – Dudu Nato May 9 '17 at 15:40

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