+ Start a Discussion
AnimeLoverAnimeLover 

how get json response into apex and access fields

Hii Friends
I want to convert JSON into APEX and access fields in it
here I am attaching my JSON before I was using serialization but I was getting heap exception 
from bellow sting, I want to access "Position" field
can you help me with this 
Thank you

{
    "summary": {
        "query": "new york united states",
        "queryType": "NON_NEAR",
        "queryTime": 100,
        "numResults": 10,
        "offset": 0,
        "totalResults": 40,
        "fuzzyLevel": 1,
        "geoBias": {
            "lat": 37.337,
            "lon": -121.89
        }
    },
    "results": [{
        "type": "Geography",
        "id": "US/GEO/p0/2602",
        "score": 10.390219688415527,
        "dist": 4102107.7960908953,
        "entityType": "Municipality",
        "address": {
            "municipality": "New York",
            "countrySubdivision": "NY",
            "countrySubdivisionName": "New York",
            "countryCode": "US",
            "country": "United States",
            "countryCodeISO3": "USA",
            "freeformAddress": "New York, NY"
        },
        "position": {
            "lat": 40.71305,
            "lon": -74.00723
        },
        "viewport": {
            "topLeftPoint": {
                "lat": 40.9175,
                "lon": -74.25564
            },
            "btmRightPoint": {
                "lat": 40.49587,
                "lon": -73.70027
            }
        },
        "boundingBox": {
            "topLeftPoint": {
                "lat": 40.9175,
                "lon": -74.25564
            },
            "btmRightPoint": {
                "lat": 40.49587,
                "lon": -73.70027
            }
        },
        "dataSources": {
            "geometry": {
                "id": "d49d86a4-0f4b-45f2-b607-31a84d02af00"
            }
        }
    }, {
        "type": "Geography",
        "id": "US/GEO/p0/1651",
        "score": 9.32979965209961,
        "dist": 3955708.622326391,
        "entityType": "Municipality",
        "address": {
            "municipality": "New York Mills",
            "countrySecondarySubdivision": "Oneida",
            "countrySubdivision": "NY",
            "countrySubdivisionName": "New York",
            "countryCode": "US",
            "country": "United States",
            "countryCodeISO3": "USA",
            "freeformAddress": "New York Mills, NY"
        },
        "position": {
            "lat": 43.09643,
            "lon": -75.30036
        },
        "viewport": {
            "topLeftPoint": {
                "lat": 43.11543,
                "lon": -75.30826
            },
            "btmRightPoint": {
                "lat": 43.08484,
                "lon": -75.28021
            }
        },
        "boundingBox": {
            "topLeftPoint": {
                "lat": 43.11543,
                "lon": -75.30826
            },
            "btmRightPoint": {
                "lat": 43.08484,
                "lon": -75.28021
            }
        },
        "dataSources": {
            "geometry": {
                "id": "0000554e-3100-3c00-0000-0000596b08c2"
            }
        }
    }, {
        "type": "Geography",
        "id": "US/GEO/p0/29888",
        "score": 9.32979965209961,
        "dist": 2404251.2708335696,
        "entityType": "Municipality",
        "address": {
            "municipality": "New York Mills",
            "countrySecondarySubdivision": "Otter Tail",
            "countrySubdivision": "MN",
            "countrySubdivisionName": "Minnesota",
            "countryCode": "US",
            "country": "United States",
            "countryCodeISO3": "USA",
            "freeformAddress": "New York Mills, MN"
        },
        "position": {
            "lat": 46.52074,
            "lon": -95.37458
        },
        "viewport": {
            "topLeftPoint": {
                "lat": 46.71649,
                "lon": -95.52331
            },
            "btmRightPoint": {
                "lat": 46.4122,
                "lon": -95.23825
            }
        },
        "boundingBox": {
            "topLeftPoint": {
                "lat": 46.71649,
                "lon": -95.52331
            },
            "btmRightPoint": {
                "lat": 46.4122,
                "lon": -95.23825
            }
        },
        "dataSources": {
            "geometry": {
                "id": "00004d4e-3200-3c00-0000-0000596a3d5c"
            }
        }
    }, {
        "type": "Geography",
        "id": "US/GEO/p0/47216",
        "score": 9.32979965209961,
        "dist": 4100442.6075305184,
        "entityType": "Municipality",
        "address": {
            "municipality": "West New York",
            "countrySecondarySubdivision": "Hudson",
            "countrySubdivision": "NJ",
            "countrySubdivisionName": "New Jersey",
            "countryCode": "US",
            "country": "United States",
            "countryCodeISO3": "USA",
            "freeformAddress": "West New York, NJ"
        },
        "position": {
            "lat": 40.78555,
            "lon": -74.00927
        },
        "viewport": {
            "topLeftPoint": {
                "lat": 40.7966,
                "lon": -74.02369
            },
            "btmRightPoint": {
                "lat": 40.77438,
                "lon": -73.99288
            }
        },
        "boundingBox": {
            "topLeftPoint": {
                "lat": 40.7966,
                "lon": -74.02369
            },
            "btmRightPoint": {
                "lat": 40.77438,
                "lon": -73.99288
            }
        },
        "dataSources": {
            "geometry": {
                "id": "0000554e-4a00-3c00-0000-000059693dac"
            }
        }
    }, {
        "type": "Street",
        "id": "US/STR/p0/10292205",
        "score": 9.056180000305176,
        "dist": 14705.030048383549,
        "address": {
            "streetName": "New York Avenue",
            "municipality": "Los Gatos",
            "countrySecondarySubdivision": "Santa Clara",
            "countrySubdivision": "CA",
            "countrySubdivisionName": "California",
            "postalCode": "95030",
            "extendedPostalCode": "950306110, 950306111, 950306112",
            "countryCode": "US",
            "country": "United States",
            "countryCodeISO3": "USA",
            "freeformAddress": "New York Avenue, Los Gatos, CA 95030",
            "localName": "Los Gatos"
        },
        "position": {
            "lat": 37.22315,
            "lon": -121.97456
        },
        "viewport": {
            "topLeftPoint": {
                "lat": 37.22378,
                "lon": -121.97598
            },
            "btmRightPoint": {
                "lat": 37.22284,
                "lon": -121.97304
            }
        }
    }, {
        "type": "Geography",
        "id": "US/GEO/p0/1",
        "score": 8.88148021697998,
        "dist": 3924591.578208102,
        "entityType": "CountrySubdivision",
        "address": {
            "countrySubdivision": "NY",
            "countrySubdivisionName": "New York",
            "countryCode": "US",
            "country": "United States",
            "countryCodeISO3": "USA",
            "freeformAddress": "NY"
        },
        "position": {
            "lat": 42.99724,
            "lon": -75.70226
        },
        "viewport": {
            "topLeftPoint": {
                "lat": 45.01585,
                "lon": -79.76259
            },
            "btmRightPoint": {
                "lat": 40.49587,
                "lon": -71.85615
            }
        },
        "boundingBox": {
            "topLeftPoint": {
                "lat": 45.01585,
                "lon": -79.76259
            },
            "btmRightPoint": {
                "lat": 40.49587,
                "lon": -71.85615
            }
        },
        "dataSources": {
            "geometry": {
                "id": "00005858-5800-1200-0000-000077360db3"
            }
        }
    }, {
        "type": "Street",
        "id": "US/STR/p0/10530766",
        "score": 8.844799995422363,
        "dist": 14325.764991750375,
        "address": {
            "streetName": "Bella Vista Avenue",
            "municipality": "Los Gatos",
            "countrySecondarySubdivision": "Santa Clara",
            "countrySubdivision": "CA",
            "countrySubdivisionName": "California",
            "postalCode": "95030, 95032",
            "extendedPostalCode": "950306101, 950306104, 950306131, 950325400, 950325415, 950325416, 950325421, 950325477",
            "countryCode": "US",
            "country": "United States",
            "countryCodeISO3": "USA",
            "freeformAddress": "Bella Vista Avenue, Los Gatos, CA",
            "localName": "Los Gatos"
        },
        "position": {
            "lat": 37.22544,
            "lon": -121.97099
        },
        "viewport": {
            "topLeftPoint": {
                "lat": 37.22907,
                "lon": -121.97304
            },
            "btmRightPoint": {
                "lat": 37.22284,
                "lon": -121.97028
            }
        }
    }, {
        "type": "Street",
        "id": "US/STR/p0/632163",
        "score": 8.735420227050781,
        "dist": 4089764.478969137,
        "address": {
            "streetName": "New York Road",
            "municipality": "Port Republic",
            "countrySecondarySubdivision": "Atlantic",
            "countrySubdivision": "NJ",
            "countrySubdivisionName": "New Jersey",
            "postalCode": "08241",
            "extendedPostalCode": "082419723, 082419724, 082419727, 082419730",
            "countryCode": "US",
            "country": "United States",
            "countryCodeISO3": "USA",
            "freeformAddress": "New York Road, Port Republic, NJ 08241",
            "localName": "Port Republic"
        },
        "position": {
            "lat": 39.54058,
            "lon": -74.46479
        },
        "viewport": {
            "topLeftPoint": {
                "lat": 39.54902,
                "lon": -74.47561
            },
            "btmRightPoint": {
                "lat": 39.53104,
                "lon": -74.45229
            }
        }
    }, {
        "type": "Street",
        "id": "US/STR/p0/648519",
        "score": 8.735420227050781,
        "dist": 4089982.9640824813,
        "address": {
            "streetName": "New York Road",
            "municipality": "Absecon",
            "countrySecondarySubdivision": "Atlantic",
            "countryTertiarySubdivision": "Galloway",
            "countrySubdivision": "NJ",
            "countrySubdivisionName": "New Jersey",
            "postalCode": "08205, 08241",
            "countryCode": "US",
            "country": "United States",
            "countryCodeISO3": "USA",
            "freeformAddress": "New York Road, Absecon, NJ",
            "localName": "Absecon"
        },
        "position": {
            "lat": 39.53321,
            "lon": -74.46423
        },
        "viewport": {
            "topLeftPoint": {
                "lat": 39.53348,
                "lon": -74.46441
            },
            "btmRightPoint": {
                "lat": 39.53304,
                "lon": -74.46405
            }
        }
    }, {
        "type": "Street",
        "id": "US/STR/p0/10748985",
        "score": 8.735420227050781,
        "dist": 2396324.9954685825,
        "address": {
            "streetName": "New York Avenue",
            "municipality": "Creston",
            "countrySecondarySubdivision": "Union",
            "countrySubdivision": "IA",
            "countrySubdivisionName": "Iowa",
            "postalCode": "50801",
            "extendedPostalCode": "508013301, 508013302, 508013303, 508013304, 508013308, 508013309, 508013310, 508013311, 508013316, 508013317, 508013320, 508013325",
            "countryCode": "US",
            "country": "United States",
            "countryCodeISO3": "USA",
            "freeformAddress": "New York Avenue, Creston, IA 50801",
            "localName": "Creston"
        },
        "position": {
            "lat": 41.04701,
            "lon": -94.37767
        },
        "viewport": {
            "topLeftPoint": {
                "lat": 41.05591,
                "lon": -94.38977
            },
            "btmRightPoint": {
                "lat": 41.03793,
                "lon": -94.36592
            }
        }
    }]
}
Arvind_SinghArvind_Singh
If you want to use specific data try JSON.createParser(jsonString) createParser(jsonString) – this would return the JSONParser class. This is useful for retrieving specific data without a need of an Apex class or if you want to parse the JSON to control how it maps to an Apex class.

Check this 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_JsonParser.htm#apex_class_System_JsonParser
 
String JSONContent = '{"firstName":"John"}';
JSONParser parser = 
   JSON.createParser(JSONContent);
// Advance to the next token.
while (parser.nextToken() != null) {
    System.debug('Current token: ' +
        parser.getCurrentToken());
}

 
AnudeepAnudeep (Salesforce Developers) 
Converted your JSON using JSON2Apex. The position field is created as follows
public GeoBias position {get;set;}    
Anudeep