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
Varsha D 9Varsha D 9 

Updating record for deserialized json

public class Api4 {
   public static void api(){
    Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://demo1691926.mockable.io/Api12');
req.setHeader('Content-Type', 'application/json');
HttpResponse response = h.send(req);
system.debug('response  : '+response.getBody());
String json1= response.getBody();

       
DesearlizingJson desearilize1=(DesearlizingJson) Json.deserialize(json1,DesearlizingJson.Class);
System.debug('Phone is ' +desearilize1.phone);
System.debug('name is ' +desearilize1.Name);
System.debug('mobile is ' +desearilize1.MobilePhone);
      
 //trying to update deserialized json       
list<contact> lcon= new list<contact>();

List<contact> lcon1 = [select id,Name,MobilePhone,Phone from contact where LastName=:desearilize1.Name];
       for(contact c1:lcon1){
        
        c1.Phone=desearilize1.phone;
           lcon.add(c1);}
        update c1;
       
  }
}

Wrapper class

public class DesearlizingJson {
    public Decimal Phone;
    public String Name;
    public Decimal MobilePhone;

}

Thanks in Advance
Suraj Tripathi 47Suraj Tripathi 47
Hi Varsha,

I've reviewed your code and it seems that your code is correct, I want to know what is your problem?
Is there any error which you are getting or your contact field is not updating please let me know what is
your issue so that I can help you?

If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi
 
Varsha D 9Varsha D 9
public class Api1 {
    public static void api(){
    Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://demo1691926.mockable.io/Api12');
req.setHeader('Content-Type', 'application/json');
HttpResponse response = h.send(req);
system.debug('response  : '+response.getBody());
String json1= response.getBody();
DesearlizingJson desearilize1=(DesearlizingJson) Json.deserialize(json1,DesearlizingJson.Class);
System.debug('Phone is ' +desearilize1.phone);
System.debug('name is ' +desearilize1.Name);
System.debug('mobile is ' +desearilize1.MobilePhone);
        
List<DesearlizingJson> dj =desearilize1.Name;   //Error :Illegal assignment from String to List<DesearlizingJson>
List<contact>  con1 = new List<contact>();

        for(DesearlizingJson c1 : dj){

    Contact c=new Contact();
    c.Name = c1.Name;    //Error:   Illegal assignment from Decimal to String
    con1.add(c);
}

insert con1;

  }
    
}

//m tryin to insert a record for the deserialized json response 

getting the above errors while conversion ....can u help me out with this 

Thanksalot in Advance
Maharajan CMaharajan C
Hi Varsha,

Please try the below updated codes:
 
public class Api1 {
    public static void api(){
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setEndpoint('https://demo1691926.mockable.io/Api12');
        req.setHeader('Content-Type', 'application/json');
        HttpResponse response = h.send(req);
        system.debug('response  : '+response.getBody());
        String json1= response.getBody();
        DesearlizingJson desearilize1=(DesearlizingJson) Json.deserialize(json1,DesearlizingJson.Class);
        System.debug('Phone is ' +desearilize1.phone);
        System.debug('name is ' +desearilize1.Name);
        System.debug('mobile is ' +desearilize1.MobilePhone);
        
        List<DesearlizingJson> dj = new List<DesearlizingJson>(); 
        dj.add(desearilize1);
        
        List<contact>  con1 = new List<contact>();
        
        for(DesearlizingJson c1 : dj){
            Contact c=new Contact();
            c.LastName = c1.Name;    
            con1.add(c);
        }
        
        insert con1;
        
    }
    
}

public class Api4 {
    public static void api(){
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setEndpoint('https://demo1691926.mockable.io/Api12');
        req.setHeader('Content-Type', 'application/json');
        HttpResponse response = h.send(req);
        system.debug('response  : '+response.getBody());
        String json1= response.getBody();
        
        
        DesearlizingJson desearilize1=(DesearlizingJson) Json.deserialize(json1,DesearlizingJson.Class);
        System.debug('Phone is ' +desearilize1.phone);
        System.debug('name is ' +desearilize1.Name);
        System.debug('mobile is ' +desearilize1.MobilePhone);
        
        //trying to update deserialized json       
        list<contact> lcon= new list<contact>();
        
        List<contact> lcon1 = [select id,Name,MobilePhone,Phone from contact where LastName=:desearilize1.Name];
        for(contact c1:lcon1){
            c1.Phone=String.ValueOf(desearilize1.phone);
            lcon.add(c1);
        }
        update lcon;
    }
}


Thanks,
Maharajan.C
Gerald BuchananGerald Buchanan

If the JSON data does not contain a value for a field, the serializer does not change that field’s value. This method allows you to keep allocations to a minimum by reusing objects that you created previously. It also allows you to “patch” objects by deliberately overwriting them with JSON that only contains a small subset of fields.

Memory usage for garbage collection (GC) is at a minimum:

ToJson allocates GC memory only for the returned string.
FromJson allocates GC memory only for the returned object, as well as any subobjects needed (for example, if DGCustomerFirst (https://www.dgcustomerfirst.ltd/) you deserialize an object that contains an array, then Unity allocates GC memory for the array).
FromJsonOverwrite allocates GC memory only as necessary for written fields (for example, strings and arrays). This means that Unity does not allocate any GC memory at all if all the fields being overwritten by the JSON are value-typed.

You can use the JsonUtility API from a background thread. However, as with any multithreaded code, be careful not to access or alter an object on one thread while another thread is serializing or deserializing it.
jhkguhf fgtrergfdjhkguhf fgtrergfd
For the case of update JSON I will suggest you to learn some bassic points about it from here (https://bradfordremovalservice.com/house-removals/) It will help you to manage it in an easy way.
gfdsn vfdngfgfdsn vfdngf
for this purpose the best option available to update in in an easy way is available here (https://apkrevolution.com/) you can click there to go to the source file.