31 votes
Accepted

How to JSON.serialize not including null values

Use the alternative method signature: serialize(objectToSerialize, suppressApexObjectNulls); Using your code as an example: String listJSON = JSON.serialize(listWrapper, true); // suppress null ...
Matthew McKinley's user avatar
14 votes
Accepted

Serialize a Map into a specific JSON object

Here how you can achieve it. map<string, object> mapToSerialize = new map<string, object>(); mapToSerialize.put('type', 'TEST'); //add you subtype like this mapToSerialize.put('subtypes', ...
Kevan's user avatar
  • 512
10 votes
Accepted

JSON Serialize and Deserialize on HttpRequest

There are certain classes that are not serializable, and HttpRequest is one of them. I can't seem to find supporting documentation, but from what I recall this is a pretty common constraint in other ...
Derek F's user avatar
  • 60.8k
8 votes

Deserialise json in apex

There is one good tool available. JSON2Apex Using that you can generate a class and using that wrapper class you can get the data. // // Generated by JSON2Apex http://json2apex.herokuapp.com/ // ...
Tushar Sharma's user avatar
8 votes

JSON deserialize from another managed package

There is a @JsonAccess annotation. Check this: https://releasenotes.docs.salesforce.com/en-us/summer20/release-notes/rn_apex_JsonAccessAnnotation.htm This annotation has 2 parameters: serializable &...
Icons's user avatar
  • 81
8 votes

JSON.serialize: is it possible to suppress null values of a map?

Yep, that's the expected behavior. For simple maps, you can get rid of null values by iterating over the map keys, keeping track of which ones have null values, and then a little map manipulation. ...
Derek F's user avatar
  • 60.8k
5 votes
Accepted

forming a JSOn structure for data table in LWC

You can return your wrapper directly to LWC, no need to serialize: public class outputwrapper { @AuraEnabled public string key; @AuraEnabled public string total; } ... @AuraEnabled public static ...
sfdcfox's user avatar
  • 487k
5 votes
Accepted

Date Json.serialize removes 00:00:00 digits from datefields of object

By default the Date type does not have a formatting method to output the ISO format. It shouldn't even do that, since it does not contain the hour, minute, second and millisecond data. You should use ...
Renato Oliveira's user avatar
4 votes
Accepted

Json serialize not giving me correct format

Using JSON2Apex very useful site to bookmark you get the following output from your listed request of: [{ "label": "Sales", "color": "#9cd159", "data": [ ["Jan", 27], ...
Eric's user avatar
  • 54.1k
4 votes
Accepted

Deserialise json in apex

You could use a JSONParser and iterate through the Tokens plucking out the "message" values... list<string> messageList = new list<String>(); JSONParser parser = JSON.createParser(...
John Thompson's user avatar
4 votes
Accepted

JSON serialize SObject including fields with null values

As per the Apex Dev Guide, Starting with API version 28.0, null fields aren’t serialized and aren’t included in the JSON string, unlike in earlier versions. This change doesn’t affect deserializing ...
Jigar Shah's user avatar
  • 1,148
3 votes
Accepted

SObject key of map mutated returns null but serializing returns the value

A map internally stores the data by hash codes, so it looks like this: { "12345" => [value1], "23456" => [value2], "34567" => [value3, value4] } When retrieving a value via get, it ...
sfdcfox's user avatar
  • 487k
3 votes
Accepted

JSON.serialize() Map<String, Object> changes Map structure

JSON doesn't know about Apex data types. The JSON library in Apex, therefore, has to be told if you want to use a specific DTO-like object. For example, if all the attributes were sObject entries, you ...
sfdcfox's user avatar
  • 487k
3 votes

JSON deserialize from another managed package

If the error message is: System.JSONException: Cannot serialize type from package: ... then a Google leads you to Dan Watt's post Salesforce: Serializing Objects from a Managed Package that ...
Keith C's user avatar
  • 136k
3 votes
Accepted

JSON.serialize: is it possible to suppress null values of a map?

The documentation for JSON.serialize(objectToSerialize, suppressApexObjectNulls) mentions the type of objectToSerialize parameter as: Type: Object The Apex object to serialize. And if you refer to ...
Jayant Das's user avatar
  • 30.4k
2 votes

Serialize a Map into a specific JSON object

May be the following sample code helps: Map<Object,List<Object>> mapTypeSubType= new Map<Object,List<Object>>(); for(Integer i=0; i<3; i++){ if(!mapTypeSubType....
Harsha's user avatar
  • 31
2 votes

JSON deserialize from another managed package

I also got this error. Optionally you can ask from the package to expose it, with the annotation @JsonAccess(serializable='always' deserializable='always') Just will add that in case the class ...
Liron C's user avatar
  • 1,776
2 votes

how to use JSON.serialize() on a wrapper class ?

You can use this code for wrapping your account list public static string getWrappedAccountList() { //Assuming you wrapper class name is 'Wrapper' List<Wrapper> WrapperList = new List&...
Ishwar Mete's user avatar
  • 1,492
2 votes
Accepted

Getting Error: System.TypeException: Invalid conversion from runtime type List<ANY> to Map<String,ANY>

The error message you're getting provides a good hint here System.TypeException: Invalid conversion from runtime type List to Map<String,ANY>` In JSON, square braces [] indicate that you're ...
Derek F's user avatar
  • 60.8k
1 vote

Not able to resolve Malformed JSON: Expected '{' at the beginning of object

My original requirment is I want to create a CPQ quote and Quote line from community site. But since salesforce is having some restriction that CPQ objects cant be accessed through Guest user license ...
SFDC buddy's user avatar
1 vote
Accepted

How can I generate arrays [...] to serialize an object? I want to make a simple JSON serialized

Assuming your question is how to generate arrays [...] using this coding pattern, the answer is to use new List<Object> for those e.g.: String payload = JSON.serialize( new Map<String, ...
Keith C's user avatar
  • 136k
1 vote
Accepted

System.JSONException: Type cannot be serialized (Can't Serialize getSlots results into a JSON string)

Some standard objects can't be serialized. You need to manually copy it to something that can be serialized. class ABS { public Decimal grade; public DateTime startTime, endTime; ABS(Decimal ...
sfdcfox's user avatar
  • 487k
1 vote

How to parse json response with multiple records and add it to list

The most simple way to do this is to use http://json2apex.herokuapp.com/. It generates you the JSON-structure that you have to Apex class(es). You can simply put your JSON String into the 'parse' ...
utm's user avatar
  • 542
1 vote
Accepted

deserialize data from stringify data

I think you want something like this: String fieldlist = '["name","isdeleted"]'; String[] lststr; lststr=(string[])JSON.deserialize(fieldlist,List<String>.class); System.debug('lststr'+...
Bryan Anderson's user avatar
1 vote

JSON.serialize: is it possible to suppress null values of a map?

This is a problem which I had faced, and I came across a string manipulation method that strips out nulls from null json keys. public static string stripJsonNulls(string JsonString) { if(...
Pranay Jaiswal's user avatar
1 vote
Accepted

How to: JSON Serialize a node/list instead of a field

I used JsonToApex. This is a free tool to perform such tasks. For that structure class would be like: public class DopplerJSONSerializer1{ public cls_items[] items; class cls_items { ...
Ashwani's user avatar
  • 22.6k
1 vote

Json serialize not giving me correct format

In JSON, [] is an array { } is a single object You want an array containing a single object. But you are not serializing an array, you are serializing a single object, myReport, that has three ...
Robert Sussland's user avatar
1 vote
Accepted

Substring for a specific value

You do not need serialization/deserialization. You can easily use sObject method get to get name of the record. For example, in your code you can use this way: for (Id key : m.keySet()) { ...
kurunve's user avatar
  • 4,289

Only top scored, non community-wiki answers of a minimum length are eligible