I'm trying to Serialize a string into JSON content but does not seems its JSON content and I have tried the following

Is there a way to convert to JSON format or you have do manually?

String jsonStr = '[image[1,2,3,4];image2:[2,23];image3:[3,4];image4:[true];image5:[true];image6:[true];]';

String JSONString = JSON.serialize(jsonStr);
system.debug('JSONString : ' +JSONString );

after executing the above in Anonymous Window I get the following result:


  • 1
    What is your exact requirement? JSON.serialize method Serializes Apex objects into JSON content.
    – Saroj Bera
    Jun 9 '16 at 10:12
  • What would you expect the output string to be? You can deserialize into an appropriate object structure if you define one.
    – Doug B
    Jun 9 '16 at 10:12
  • @SarojBera: yes thats what I read but I'm not sure if it also Serializes string, my requirement is to convert that string into json format.
    – Nick Kahn
    Jun 9 '16 at 10:13
  • @Nick you are converting a string into a string, what do you think it would look like?
    – Doug B
    Jun 9 '16 at 10:14
  • @DougB: I expect the output to be JSON format, I do not have any object, but I will be storing the json string into one of my custom field
    – Nick Kahn
    Jun 9 '16 at 10:15

It's not clear what the output String you are expecting is. The contents of the String are JSON so perhaps you are wanting to expand those into an object?

If you can express what you think the output string should look like, then we may be able to better help? You want an output that is a String, but somehow changed from the original String?

Perhaps you are looking for something like this

public class AWrapper{  
    String jsonStr;

aWrapper nickWrapper = new AWrapper();
nickWrapper.jsonStr = '[image[1,2,3,4];image2:[2,23];image3:[3,4];image4:[true];image5:[true];image6:[true];]';
String JSONString = JSON.serialize(NickWrapper);
system.debug('JSONString : ' +JSONString );

So the output looks like

  • 1+ perfect thats what I was looking for :)
    – Nick Kahn
    Jun 9 '16 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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