1

I have a JSON String as below.

{"title":"ZoomMeetingfortesting","userId":"[email protected]","startTime":"2022-06-30T00:05:00.000Z","participatEmailList":{0},"endTime":"2022-06-23T00:25:00.000Z","settings":{"registration_type":0,"join_before_host":true,"registrants_email_notification":true,"registrants_confirmation_email":true}}

But when I try to use a formatter here.

public class ZoomParticipant{
    public String email;
}

List<ZoomParticipant> zoomParticipants = new List<ZoomParticipant>();
ZoomParticipant participant = new ZoomParticipant();
participant.email = '[email protected]';
zoomParticipants.add(participant);


String str = String.format(str1, new List<String>{
        JSON.serialize(zoomParticipants)
});

It fails with

Error on line 13, column 1: System.StringException: Bad argument syntax: [at pattern index 1] ""title":"ZoomMeeting ..."
AnonymousBlock: line 13, column 1

Because in JSON we already have { and },

To resolve this in My JSON I replaced { and } with some other characters. Like this and it works fine.

String str1 = '****"title":"ZoomMeetingfortesting","userId":"[email protected]","startTime":"2022-06-30T00:05:00.000Z","participatEmailList":{0},"endTime":"2022-06-23T00:25:00.000Z","settings":****"registration_type":0,"join_before_host":true,"registrants_email_notification":true,"registrants_confirmation_email":true&&&&&&&&';

public class ZoomParticipant{
    public String email;
}

List<ZoomParticipant> zoomParticipants = new List<ZoomParticipant>();
ZoomParticipant participant = new ZoomParticipant();
participant.email = '[email protected]';
zoomParticipants.add(participant);


String str = String.format(str1, new List<String>{
        JSON.serialize(zoomParticipants)
});


str = str.replace('****', '{');
str = str.replace('&&&&', '}');

System.debug(str);

18:11:43.20 (26014201)|USER_DEBUG|[21]|DEBUG|{"title":"ZoomMeetingfortesting","userId":"[email protected]","startTime":"2022-06-30T00:05:00.000Z","participatEmailList":[{"email":"[email protected]"}],"endTime":"2022-06-23T00:25:00.000Z","settings":{"registration_type":0,"join_before_host":true,"registrants_email_notification":true,"registrants_confirmation_email":true}}

But is there a proper way to handle it, so that I dont have to change JSON at source?

1 Answer 1

0

If you're generating the rest of the JSON within Salesforce, then it'd make more sense (I think) to either just build this in to the class or Map<String, Object> that you're using to get the JSON in the first place.

e.g.

List<ZoomParticipant> zoomParticipants = new List<ZoomParticipant>();
ZoomParticipant participant = new ZoomParticipant();
participant.email = '[email protected]';
zoomParticipants.add(participant);

Map<String, Object> myJSONMap = new Map<String, Object>{
    'title' => 'Zoom Meeting test',
    'startTime' => Datetime.now(),
    'participantEmailList' => zoomParticipants,
    'settings' => new Map<String, Object>{
        'registration_type' => 0
        ...
    }
};

Stirng myJSON = JSON.serialize(myJSONMap);

If you can't control the rest of the JSON string, then I'd use String.replace() instead of String.format() here.

str1 = str1.replace('{0}', JSON.serialize(zoomParticipants));
1
  • 1
    No I dont want to build JSON in apex, I want to keep it in Custom metatdata level and all dynamic pieces I would like to add within apex. Also replace wont exactly help me here, as it cannot take mutiple replacements. In JSON I can have multiple dynamic values to be replaced. Jun 30, 2022 at 13:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .