function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Cloud AtlasCloud Atlas 

Adding an Array within Map for JSON payload

I am trying to send a record to an external system via Async service . Only one value within that payload needs to be an array while the remaining should be string. Does any one know how it can be done?
searlization looks like below..
Map<String, String> data = new Map<String, String>{
            'email' => acc.Email__c,
            'displayName' => acc.Display_Name__c,
            'bioUrl' => acc.Bio_URL__c,
            'imageUrl' => acc.Image_URL__c,
            'vertical' => acc.Channel__c,
            'status' => acc.Status__c,
            'gid' => acc.Legacy_GID__c,
            'numberOfExperts' => String.valueOf(acc.Number_of_Experts__c),
            'userId' => String.valueOf(acc.Selene_ID__c),
            'socialPreference' => acc.Social_Presence__c
        };
        String jsonInput = JSON.serializePretty(data, True);

While the output that I am seeking is like..
{
  "socialPreference" : [
                    "www.facebook.com/101", 
                    "www.twitter.com/101"
                        ],
  "userId" : "123456789123",
  "numberOfExperts" : "1",
  "gid" : "210967",
  "status" : "LIVE",
  "vertical" : "SALESFORCE",
  "imageUrl" : "",
  "bioUrl" : "",
  "displayName" : "test101",
  "email" : "test101@test.com"
}
I don't know how to convert just one value (socialPresence)into an array format.
That is a LONG TEXT field in UI and will hold multiple urls separated by comma or semi-colon.

 
Best Answer chosen by Cloud Atlas
Niraj Kr SinghNiraj Kr Singh
Hi,

you can try like this: As for testing purpose i have added ArrayPreferences and get as expected
List<String> lstPreferences = new List<String>(); //Add you all socialPreference here in list/array and used it as for testing purpose i have added ArrayPreferences and get as expected.
lstPreferences.add('abc.com');
lstPreferences.add('xyz.com');
Map<String, String> data = new Map<String, String>{
            'email' => acc.Email__c,
            'displayName' =>'acc.Display_Name__c,
            'bioUrl' => acc.Bio_URL__c,
            'imageUrl' => acc.Image_URL__c,
            'vertical' => 'acc.Channel__c',
            'status' => acc.Status__c,
            'gid' => acc.Legacy_GID__c,
            'numberOfExperts' => String.valueOf(acc.Number_of_Experts__c),
            'userId' => String.valueOf(acc.Selene_ID__c),
            'socialPreference' => acc.Social_Presence__c,
            'ArrayPreferences' => JSON.serializePretty(lstPreferences, True)
        };
Plz let me if it helps you.
Thanks
Niraj
 

All Answers

Niraj Kr SinghNiraj Kr Singh
Hi,

you can try like this: As for testing purpose i have added ArrayPreferences and get as expected
List<String> lstPreferences = new List<String>(); //Add you all socialPreference here in list/array and used it as for testing purpose i have added ArrayPreferences and get as expected.
lstPreferences.add('abc.com');
lstPreferences.add('xyz.com');
Map<String, String> data = new Map<String, String>{
            'email' => acc.Email__c,
            'displayName' =>'acc.Display_Name__c,
            'bioUrl' => acc.Bio_URL__c,
            'imageUrl' => acc.Image_URL__c,
            'vertical' => 'acc.Channel__c',
            'status' => acc.Status__c,
            'gid' => acc.Legacy_GID__c,
            'numberOfExperts' => String.valueOf(acc.Number_of_Experts__c),
            'userId' => String.valueOf(acc.Selene_ID__c),
            'socialPreference' => acc.Social_Presence__c,
            'ArrayPreferences' => JSON.serializePretty(lstPreferences, True)
        };
Plz let me if it helps you.
Thanks
Niraj
 
This was selected as the best answer
Cloud AtlasCloud Atlas
This worked.
I did separate out values in different fields .. abc.com in one field and xyz.com in another and then just combined them in list.
But your approach is brilliant.
Thanks Niraj.
Really appreciate the assist.