[SalesForce] updating json response salesforce

{ “FirstName” : “Foo”, 
  “LastName” : “Bar”, 
  “Address” : 
       { “city” : “Pune”, “state” : “Maharashtra”, “country” : “India” } 
}

My issues is to fetch the "city" value from the above json format ,and update it to "USA"(say).

what could be the possible solution?
The above json format I am getting by using response.getbody().

Like I am using

Map<string,object> objmap= (Map<string,object>)json.deserializeuntype(response.getbody());
system.debug(objmap.get('Address'));

So, from here now i have to update city="USA".

Actual Json:—

    {"size":1,
"totalSize":1,
"done":true,
"queryLocator":null,
"entityTypeName":"FlowDefinition",
"records":[{"attributes":{"type":"FlowDefinition","url":"/services/data/v42.0/tooling/sobjects/FlowDefinition/3007F000000LP9xQAG"},
"FullName":"test_flow",
"DeveloperName":"test_flow",
"Metadata":{"activeVersionNumber":null,"description":null,"masterLabel":null,"urls":null}}]}

As we can see the field activeVersionNumber is null,so we need to set it as 0 or 1.
The return type of activeVersionNumber is object.
If i use Json2Apex ,the return type is Object but i need to set the integer value in it.

Best Answer

You can convert JSON into equivalent Apex class like what @Shantanu has suggested, the only downside is sometimes we dont know the JSON structure to create Apex classes.

Instead, we can use deserializeuntype . deserializeuntype returns us the Map keyValue pair , which we can use on our advantage, and alter the value in map and again serialize it.

String input = '{"size":1,"totalSize":1,"done":true,"queryLocator":null,"entityTypeName":"FlowDefinition","records":[{"attributes":{"type":"FlowDefinition","url":"/services/data/v42.0/tooling/sobjects/FlowDefinition/3007F000000LP9xQAG"},"FullName":"test_flow","DeveloperName":"test_flow","Metadata":{"activeVersionNumber":null,"description":null,"masterLabel":null,"urls":null}}]}';

Map<string,object> objmap= (Map<string,object>)json.deserializeUntyped(input );

List<Object> recordsMap =(List<Object>) objmap.get('records');

for(Object obj: recordsMap ){
    Map<String,Object> mapOfrecord = ( Map<String,Object>)obj;
    Map<String,Object> metaDataObject =( Map<String,Object>)  mapOfrecord.get('Metadata'); 
    metaDataObject.put('activeVersionNumber',1);

}


System.debug(JSON.serializePretty(objmap));

Output =>

  {
  "records" : [ {
    "Metadata" : {
      "urls" : null,
      "masterLabel" : null,
      "description" : null,
      "activeVersionNumber" : 1
    },
    "DeveloperName" : "test_flow",
    "FullName" : "test_flow",
    "attributes" : {
      "url" : "/services/data/v42.0/tooling/sobjects/FlowDefinition/3007F000000LP9xQAG",
      "type" : "FlowDefinition"
    }
  } ],
  "entityTypeName" : "FlowDefinition",
  "queryLocator" : null,
  "done" : true,
  "totalSize" : 1,
  "size" : 1
}

Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm

Related Topic