I get back JSON like the following:
{
Id = a0Ef0000002TPKVEA4;
Name = a0Ef0000002TPKV;
attributes = {
type = "Market__c";
url = "/services/data/v29.0/sobjects/Market__c/a0Ef0000002TPKVEA4";
};
The attributes portion doubles the data size. I've searched the docs but can't find a way to remove it. Here is the code I am using
@HttpGet
global static Aggregate doGet() {
RestRequest req = RestContext.request;
string userid = req.params.get('UserId');
try {
User user = [SELECT FirstName, LastName, Title, Email, Phone, MobilePhone,
Street, City, State, PostalCode, Country, LevelOfCare__c, Username, PERNER__c
FROM User WHERE Id = :userid];
List<Market__c> markets = [SELECT Id, Name FROM Market__c];
return new Aggregate(user, markets);
}
catch (exception e) {
return null;
}
}
global class Aggregate {
public User user {get; set;}
public List<Market__c> markets {get; set;}
public Aggregate(User u, List<Market__c> m) {
user = u;
markets = m;
}
} }
Attribution to: Brad Thomas
Possible Suggestion/Solution #1
Convert the object into a string and do not use standard salesforce serialize method.
Ex:
Account a = new Account(); String resString = String.valueof(a); return resString;
In this way we could not have \ in the response.
Attribution to: Elavarasan N
Possible Suggestion/Solution #2
You can simply use JSONGenerator to get rid of the "attributes" element.
Refer to this link: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsongenerator.htm
Attribution to: Irfan Shk
Possible Suggestion/Solution #3
It's a bit hacky, but you could remove the attributes by serialising and deserialising the SObjects to Maps, like this example:
Account acc = [Select Id, Name, (Select Id, Name From Contacts) From Account LIMIT 1];
Map<String,Object> accMap = (Map<String,Object>)JSON.deserializeUntyped(JSON.serialize(acc));
removeAttributes(accMap);
System.debug(JSON.serializePretty(accMap)); // et voila, no attributes
private void removeAttributes(Map<String,Object> jsonObj) {
for(String key : jsonObj.keySet()) {
if(key == 'attributes') {
jsonObj.remove(key);
} else {
if(jsonObj.get(key) instanceof Map<String,Object>) {
removeAttributes((Map<String,Object>)jsonObj.get(key));
}
if(jsonObj.get(key) instanceof List<Object>) {
for(Object listItem : (List<Object>)jsonObj.get(key)) {
if(listItem instanceof Map<String,Object>) {
removeAttributes((Map<String,Object>)listItem);
}
}
}
}
}
}
Probably not any more palatable that @KeithC's answer, but an alternative.
Attribution to: executeanon
Possible Suggestion/Solution #4
As it is custom Apex code, instead of directly returning the SObject, return a simple "bean" class that just has an Id and Name property with the values copied from the SObject:
public class Bean {
public Id Id;
public String Name;
Bean(SObject sob) {
this.Id = sob.Id;
this.Name = (String) sob.get('Name');
}
}
PS
Based on the posted code, it is the nested SObjects that you need to eliminate:
global class Aggregate {
public Bean user {get; set;}
public List<Bean> markets {get; set;}
public Aggregate(User u, List<Market__c> ms) {
user = new Bean(u);
markets = new List<Bean>();
for (Market__c m : ms) {
markets.add(new Bean(m));
}
}
}
Attribution to: Keith C
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33690