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
Sam WardSam Ward 

remove fields from JSON output.

Hi,

I'm wanting to remove currency, ID, and recordtypeid from my JSON file but I can't seem to find a way to remove them from the JSON file, Can someone advise where / What I need to be looking at to. Please see the code below.
 
public class JsonGenDispositionData {
    
    public String jsonString {set;get;}
    
    public static void gatherDispositionData(){
            
        List<stage_tracker__c> dispositionDataToSend = [SELECT Distribution_id__c,contact_status__c FROM Stage_Tracker__c WHERE Distribution_id__c != null LIMIT 3];
        List<Stage_tracker__c> payload = new list <Stage_Tracker__c>();
        
        // system.debug('Stage Trackers' + dispositionDataToSend);
        
        If(dispositionDataToSend.isempty()==false){
            JSONGenerator gen = JSON.createGenerator(true);
            for(Stage_Tracker__c ST : dispositionDataToSend){
                gen.writeStartArray();
                gen.writeStartObject();
                gen.writeStringField('DISTRIBUTION_ID', ST.Distribution_Id__c);
                gen.writeBooleanField('CONTACT_STATUS', ST.contact_status__c);
                gen.writeEndObject();
                gen.writeEndArray();
                payload.add(ST); 
            }
        }
        
        string jsonString = JSON.serialize(payload);
        system.debug(jsonString);
    }
    
}

My currnet output:
 
[{"attributes":{"type":"Stage_Tracker__c","url":"/services/data/v53.0/sobjects/Stage_Tracker__c/a5i3z000000UKNZAA4"},
"Distribution_Id__c":"8056fec2-b009-2cde-2d98-5f9ad4",
"contact_status__c":true,
"Id":"a5i3z000000UKNZAA4",
"RecordTypeId":"0123z000000gHwCAAU",
"CurrencyIsoCode":"GBP"}]
 I dont need the last 3 lines, (Id / RecordTypeId / CurrencyIsoCode)

How can I remove them.

Also any general code improvments would be welcomed.

Thanks
​​​​​​​
CharuDuttCharuDutt
Hii San Ward 
Try Below Code
public class RemoeAttFromJson {
    public static void method (){
        Account acc = [Select Id, Name, (Select Id, Name From Contacts) From Account LIMIT 1];
        Map<String,Object> jsonObj = (Map<String,Object>)JSON.deserializeUntyped(JSON.serialize(acc));
        System.debug('Before//// '+JSON.serializePretty(jsonObj)); // et voila, no attributes
        removeAttributes(jsonObj);
    }
    public static void removeAttributes(Map<String,Object> jsonObj){
        
     for(String key : jsonObj.keySet()) {
        if(key == 'Name') {
            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);
                    }
                }
            }
        }
    }
        System.debug('After//// '+JSON.serializePretty(jsonObj));
}
}
Please Mark It As Best Answer If It Helps
Thank You!
anjali telikepallianjali telikepalli
Hi @CharuDutt ,
In my case i want to fetch list of record, but it's is fetching only one record.
receving below error "Invalid conversion from runtime type List<ANY> to Map<String,ANY>".
Could you please help me how to resolve it.

Thanks in Advance..