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

Serialize String to JSON

I have a String which I need to convert and make it look the below in JSON.
I want to convert the String s to JSON, Please help

String allIndexes = 'Columns/Sections :1, Columns/Sections :2';

String s = 'error: "SECTION_LENGTH_EXCEEDED",' +'\n'+'message: Unable to save RuleAction record, HTML content size is bigger that expected ' +'\n'+'erroredSections : '+ allIndexes;

JSON : {
    message: "Unable to save sections, json object size is bigger that expected",
        erroredSections: [
          { Columns/Sections :1 }
      { Columns/Sections :2 }

E Jayaraman IyerE Jayaraman Iyer

You can create a wrapper for your json and then use that wrapper to create the object as you want and then serialise it. It will give you exact json as you want.

Wrapper may look something like this : 
public class fromJSON{
	public String error;	
	public String message;
	public List<cls_erroredSections> erroredSections;
	class cls_erroredSections {
		public String Columns/Sections;