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
hiteshwar marnihiteshwar marni 

json parse

How to parse this JSON


{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "111",
                    "short_name": "111",
                    "types": [
                        "street_number"
                    ]
                },
                {
                    "long_name": "Main Street",
                    "short_name": "Main St",
                    "types": [
                        "route"
                    ]
                },
                {
                    "long_name": "Old East Dallas",
                    "short_name": "Old East Dallas",
                    "types": [
                        "neighborhood",
                        "political"
                    ]
                },
                {
                    "long_name": "Dallas",
                    "short_name": "Dallas",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "Dallas County",
                    "short_name": "Dallas County",
                    "types": [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    "long_name": "Texas",
                    "short_name": "TX",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "United States",
                    "short_name": "US",
                    "types": [
                        "country",
                        "political"
                    ]
                },
                {
                    "long_name": "75226",
                    "short_name": "75226",
                    "types": [
                        "postal_code"
                    ]
                }
            ],
            "formatted_address": "111 Main St, Dallas, TX 75226, USA",
            "geometry": {
                "location": {
                    "lat": 32.7856754,
                    "lng": -96.7732783
                },
                "location_type": "RANGE_INTERPOLATED",
                "viewport": {
                    "northeast": {
                        "lat": 32.7870243802915,
                        "lng": -96.77192931970849
                    },
                    "southwest": {
                        "lat": 32.7843264197085,
                        "lng": -96.77462728029151
                    }
                }
            },
            "partial_match": true,
            "place_id": "EiIxMTEgTWFpbiBTdCwgRGFsbGFzLCBUWCA3NTIyNiwgVVNBIhoSGAoUChIJdcuXU8CYToYRqT7JZFXIPrEQbw",
            "types": [
                "street_address"
            ]
        },
        {
            "address_components": [
                {
                    "long_name": "602",
                    "short_name": "602",
                    "types": [
                        "street_number"
                    ]
                },
                {
                    "long_name": "Main Street",
                    "short_name": "Main St",
                    "types": [
                        "route"
                    ]
                },
                {
                    "long_name": "West End",
                    "short_name": "West End",
                    "types": [
                        "neighborhood",
                        "political"
                    ]
                },
                {
                    "long_name": "Dallas",
                    "short_name": "Dallas",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "Dallas County",
                    "short_name": "Dallas County",
                    "types": [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    "long_name": "Texas",
                    "short_name": "TX",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "United States",
                    "short_name": "US",
                    "types": [
                        "country",
                        "political"
                    ]
                },
                {
                    "long_name": "75202",
                    "short_name": "75202",
                    "types": [
                        "postal_code"
                    ]
                }
            ],
            "formatted_address": "602 Main St, Dallas, TX 75202, USA",
            "geometry": {
                "location": {
                    "lat": 32.7791016,
                    "lng": -96.8065259
                },
                "location_type": "RANGE_INTERPOLATED",
                "viewport": {
                    "northeast": {
                        "lat": 32.78045058029149,
                        "lng": -96.8051769197085
                    },
                    "southwest": {
                        "lat": 32.7777526197085,
                        "lng": -96.80787488029151
                    }
                }
            },
            "partial_match": true,
            "place_id": "EiI2MDIgTWFpbiBTdCwgRGFsbGFzLCBUWCA3NTIwMiwgVVNBIhsSGQoUChIJ_yeoUBaZToYRaoXTXGzpnc0Q2gQ",
            "types": [
                "street_address"
            ]
        }
    ],
    "status": "OK"
}


I want to access DALLAS COUNTY
it's urgent 


Thank you
VamsiVamsi
Hi,

If you would like to parse this JSON via Apex then please go through the link https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsonparser.htm

Please mark as best answer if the above helps...!!!
Raj VakatiRaj Vakati
 Consider using JSON2Apex in the future to automatically generate the correct code from your JSON as wrapper class and you could able use the wrapper class in the front end or other places 



 Consider using JSON2Apex in the future to automatically generate the correct code from your JSON.


Here is the wrapper class for the  JSON
//
//Generated by AdminBooster
//

public class fromJSON{
	public cls_results[] results;
	public String status;	//OK
	class cls_results {
		public cls_address_components[] address_components;
		public String formatted_address;	//111 Main St, Dallas, TX 75226, USA
		public cls_geometry geometry;
		public boolean partial_match;
		public String place_id;	//EiIxMTEgTWFpbiBTdCwgRGFsbGFzLCBUWCA3NTIyNiwgVVNBIhoSGAoUChIJdcuXU8CYToYRqT7JZFXIPrEQbw
		public cls_types[] types;
	}
	class cls_address_components {
		public String long_name;	//111
		public String short_name;	//111
		public cls_types[] types;
	}
	class cls_types {
		public String 0;	//s
		public String 1;	//t
		public String 2;	//r
		public String 3;	//e
		public String 4;	//e
		public String 5;	//t
		public String 6;	//_
		public String 7;	//a
		public String 8;	//d
		public String 9;	//d
		public String 10;	//r
		public String 11;	//e
		public String 12;	//s
		public String 13;	//s
	}
	class cls_geometry {
		public cls_location location;
		public String location_type;	//RANGE_INTERPOLATED
		public cls_viewport viewport;
	}
	class cls_location {
		public Double lat;	//32.7856754
		public Double lng;	//-96.7732783
	}
	class cls_viewport {
		public cls_northeast northeast;
		public cls_southwest southwest;
	}
	class cls_northeast {
		public Double lat;	//32.7870243802915
		public Double lng;	//-96.77192931970849
	}
	class cls_southwest {
		public Double lat;	//32.7843264197085
		public Double lng;	//-96.77462728029151
	}
	public static fromJSON parse(String json){
		return (fromJSON) System.JSON.deserialize(json, fromJSON.class);
	}

	
}




deserialization as shown below 
 
fromJSON jsonW = (fromJSON) System.JSON.deserialize(json, fromJSON.class);

 
Narender Singh(Nads)Narender Singh(Nads)
Hi,
To parse this JSON, first paste the following code in global scope of your main class:
 
public class JsonClass{
	public cls_results[] results;
	public String status;	//OK
	class cls_results {
		public cls_address_components[] address_components;
		public String formatted_address;	//111 Main St, Dallas, TX 75226, USA
		public cls_geometry geometry;
		public boolean partial_match;
		public String place_id;	//EiIxMTEgTWFpbiBTdCwgRGFsbGFzLCBUWCA3NTIyNiwgVVNBIhoSGAoUChIJdcuXU8CYToYRqT7JZFXIPrEQbw
		public cls_types[] types;
	}
	class cls_address_components {
		public String long_name;	//111
		public String short_name;	//111
		public cls_types[] types;
	}
	class cls_types {
		public String 0;	//s
		public String 1;	//t
		public String 2;	//r
		public String 3;	//e
		public String 4;	//e
		public String 5;	//t
		public String 6;	//_
		public String 7;	//a
		public String 8;	//d
		public String 9;	//d
		public String 10;	//r
		public String 11;	//e
		public String 12;	//s
		public String 13;	//s
	}
	class cls_geometry {
		public cls_location location;
		public String location_type;	//RANGE_INTERPOLATED
		public cls_viewport viewport;
	}
	class cls_location {
		public Double lat;	//32.7856754
		public Double lng;	//-96.7732783
	}
	class cls_viewport {
		public cls_northeast northeast;
		public cls_southwest southwest;
	}
	class cls_northeast {
		public Double lat;	//32.7870243802915
		public Double lng;	//-96.77192931970849
	}
	class cls_southwest {
		public Double lat;	//32.7843264197085
		public Double lng;	//-96.77462728029151
	}
	
}

Now, the next step is to deserialize your response. To do that write this line in your class function where you want to deserialize this response.

string JSONResponse;//STORE YOUR RESPONSE HERE
JsonClass obj= (JsonClass) System.JSON.deserialize(JSONResponse, JsonClass.class);


Next you have to traverse the JSON object you just created using for loop.
Something Like this:
 
for(cls_results cls_resultsObject:JsonClass.results ){

       for(cls_address_components cls_address_componentsObject : cls_resultsObject.address_components){
               if(cls_address_componentsObject.long_name=='Dallas County'){
                    system.debug(cls_address_componentsObject);
                    //Rest of the logic depends on your requirement
               }
       }
}