Search code examples
javajsonapexapex-code

JSON formatting in Apex


I am getting a JSON in below format:

{
"A":"1",
"B":"2"
}

I have a field update and JSON at some point could be too long. How can I change the JSON format to below pattern?

{"A":"1","B":"2"}

I am trying to store this minified JSON format on a field, so that char limit issue is resolved.


Solution

  • deserialize the string and serialize it again and then update the field. Declare the JSON as string

    Map<String,Object> parser = (Map<String,Object>)JSON.deserializeUntyped(yourstring);
    yourstring = JSON.serialize(parser);
    

    You can change Map accordingly. Required format will be present in string variable.