{ “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());

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

Actual Json:---


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.


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'); 



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


With the use of this JSON2Apex, you can convert the JSON to Apex class as follows

public class JSON2Apex {

    public String FirstName;
    public String LastName;
    public Address Address;

    public class Address {
        public String city;
        public String state;
        public String country;

    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);

Now you can assign value easily.

JSON2Apex obj = JSON2Apex.parse(response.getbody());
obj.Address.city = 'USA';

System.debug('city='+ obj.Address.city);
  • ,suppose in the above class inside address class there is one more variable like public Object Activeversionnumber; then how to set this value as 0 or 1; return type is object and we want to set it to integer.
    – vineet
    May 14 '18 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.