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
Keith Stephens 18Keith Stephens 18 

need json formatting help

Hello All,
I hope someone can help me to format my JSON string This is what I have.
String tmpSetBody = '{\"personalizations\":[{\"to\":[{\"email\":\"'+recipient+'\",\"name\":\"'+recipient+'\"}],\"subject\":\"TestTestTest '+emailSubject + ' ' + candidate+'\"}],\"from\":{\"email\":\"support@email.com\",\"name\":\"Support\"},\"reply_to\":{\"email\":\"support@email.com\",\"name\":\"support\"},\"content\":[{\"type\":\"text/plain\",\"value\":\"'+body+'"\"}]})';
This is the result when using the system.debug and the development console:
tmpSetBody= {"personalizations":[{"to":[{"email":"myemail@mail.com","name":"First Name"}],"subject":"TestTestTest Modify existing card:  123"}],"from":{"email":"support@email.com","name":"Support"},"reply_to":{"email":"support@Email.com","name":"support"},"content":[{"type":"text/plain","value":"Modify  card: 123 Card Limit: 550.00

And this is how it should look:
tmpSetBody2= {"personalizations":[{"to":[{"email":"myemail@glofin.com","name":"support"}],"subject":"TestTestTest Modify existing card:  123"}],"from":{"email":"support@email.com","name":"support"},"reply_to":{"email":"support@email.com","name":"support"},"content":[{"type":
"text/plain","value":"Modify  card: 123 Card Limit: 550.00"}]})

It seems I am missing the ending brackets and quote, but everything I have tried does not seem to work.
Thanks,
Keith.
 
Best Answer chosen by Keith Stephens 18
Ingrid Vanherpen 5Ingrid Vanherpen 5
Yes that is possibly the new line character from the email body
 
 

All Answers

Ingrid Vanherpen 5Ingrid Vanherpen 5
...lue\":\"'+body+'   "   \"}]})';      The pattern is \ before a " when inside ' single quotes'  ( so \") to make it display the  " .   At the end of you statement it seems you have added an extra " (I isolated and put in bold) that is not correct. Remove the extra " before the \"
Keith Stephens 18Keith Stephens 18
I am sorry I don't follow, I have tryied removing the " but then the whole JSON string is incorrect. Can you correct what I have?
Ingrid Vanherpen 5Ingrid Vanherpen 5
When I correct the string to this String tmpSetBody = '{\"personalizations\":[{\"to\":[{\"email\":\"'+recipient+'\",\"name\":\"'+recipient+'\"}],\"subject\":\"TestTestTest '+emailSubject + ' ' + candidate+'\"}],\"from\":{\"email\":\"support@email.com\",\"name\":\"Support\"},\"reply_to\":{\"email\":\"support@email.com\",\"name\":\"support\"},\"content\":[{\"type\":\"text/plain\",\"value\":\"'+body+'\"}]})’; then debug gives 16:15:18:001 USER_DEBUG [6]|DEBUG|{"personalizations":[{"to":[{"email":"myemail@mail.com","name":"myemail@mail.com"}],"subject":"TestTestTest TestTestTest Modify existing card: 123 support@email.com"}],"from":{"email":"support@email.com","name":"Support"},"reply_to":{"email":"support@email.com","name":"support"},"content":[{"type":"text/plain","value":"Modify card: 123 Card Limit: 550.00"}]}) Which is what you wanted no ? The problem is the red “ (see below) - String tmpSetBody = '{\"personalizations\":[{\"to\":[{\"email\":\"'+recipient+'\",\"name\":\"'+recipient+'\"}],\"subject\":\"TestTestTest '+emailSubject + ' ' + candidate+'\"}],\"from\":{\"email\":\"support@email.com\",\"name\":\"Support\"},\"reply_to\":{\"email\":\"support@email.com\",\"name\":\"support\"},\"content\":[{\"type\":\"text/plain\",\"value\":\"'+body+'"\"}]})';
Keith Stephens 18Keith Stephens 18
I did see that I had a ) at the end of the string, but still that did not fix it.
String tmpSetBody = '{\"personalizations\":[{\"to\":[{\"email\":\"'+recipient+'\",\"name\":\"'+recipient+'\"}],\"subject\":\"TestTestTest '+emailSubject + ' ' + candidate+'\"}],\"from\":{\"email\":\"support@email.com\",\"name\":\"Support\"},\"reply_to\":{\"email\":\"support@email.com\",\"name\":\"support\"},\"content\":[{\"type\":\"text/plain\",\"value\":\"'+body+'"\"}]}';
This is what I have now, but it's still not ending the JSON.
{"personalizations":[{"to":[{"email":"kstephens@glofin.com","name":"myemail@email.com"}],"subject":"TestTestTest Modify existing card:  123"}],"from":{"email":"support@email.com","name":"Support"},"reply_to":{"email":"support@email.com","name":"support"},"content":[{"type":"text/plain","value":"Modify existing card: 123 Card Limit: 887.00

 
Ingrid Vanherpen 5Ingrid Vanherpen 5
The ending of the string I sent is    body+'\"}]})       what you show as declaration of the tmpSetBody is still body+'"\"}]})  (so one " too many). From the info you have provided (I do not see the rest of your code), I simulated the declaration in executeAnonymous and with that declaration the result is of sending tmpSetBody to debug (showing a snapshot of the end :
 User-added image
So if you have that exact formatting it must be that other parts of the code interact or are incorrect. The string tmpSetBody seems cut off. Is there a lenght limit playing up ? 
 
Ingrid Vanherpen 5Ingrid Vanherpen 5
Also if you are constructing the string from an actual email body the body variable may contain a new line character. Could be that this gives the truncation aspect in debug. Where is body coming from, what does it look like ? Have you tried instead of body to put some fix characters and see if body is the culprit ?
 
Keith Stephens 18Keith Stephens 18
I am not setting any line lenght.
This is what I have now, which is what you side you have  body+'\"}]}.
String tmpSetBody = '{\"personalizations\":[{\"to\":[{\"email\":\"'+recipient+'\",\"name\":\"'+recipient+'\"}],\"subject\":\"TestTestTest '+emailSubject + ' ' + candidate+'\"}],\"from\":{\"email\":\"support@email.com\",\"name\":\"Support\"},\"reply_to\":{\"email\":\"support@email.com\",\"name\":\"support\"},\"content\":[{\"type\":\"text/plain\",\"value\":\"'+ body+'\"}]}';
But what is funny is that I am only doing a System.Debug(tmpSetBody); and it's displays on 2 lines in the developer console window?

User-added image
 
Ingrid Vanherpen 5Ingrid Vanherpen 5
Yes that is possibly the new line character from the email body
 
 
This was selected as the best answer
Keith Stephens 18Keith Stephens 18
Yes, the body does have a \n new line in it.
Ingrid Vanherpen 5Ingrid Vanherpen 5
you might want to remove that one as it seems to interfere
 
 
Keith Stephens 18Keith Stephens 18
I am going to try and escape it first, but yes I can remove it if need be.
Thanks for all of your time and help.
Ingrid Vanherpen 5Ingrid Vanherpen 5
no worries, good luck!