Search code examples
c#.netjsonjson.net

Can I Deserialize a specific field from JSON?


I am writing a method that takes a British post code and returns the Latitude and Longitude of this postcode as a "Location" object.

public class Location
{
    public double Longitude;
    public double Latitude;
}

I am using the API from https://postcodes.io/ that takes a postcode and returns a fairly large set of data about it. For example (Buckingham Palace if you're interested):

{
"status": 200,
"result": {
    "postcode": "SW1A 1AA",
    "quality": 1,
    "eastings": 529090,
    "northings": 179645,
    "country": "England",
    "nhs_ha": "London",
    "longitude": -0.141587597876975,
    "latitude": 51.5010091564599,
    "european_electoral_region": "London",
    "primary_care_trust": "Westminster",
    "region": "London",
    "lsoa": "Westminster 018C",
    "msoa": "Westminster 018",
    "incode": "1AA",
    "outcode": "SW1A",
    "parliamentary_constituency": "Cities of London and Westminster",
    "admin_district": "Westminster",
    "parish": "Westminster, unparished area",
    "admin_county": null,
    "admin_ward": "St James's",
    "ccg": "NHS Central London (Westminster)",
    "nuts": "Westminster",
    "codes": {
        "admin_district": "E09000033",
        "admin_county": "E99999999",
        "admin_ward": "E05000644",
        "parish": "E43000236",
        "parliamentary_constituency": "E14000639",
        "ccg": "E38000031",
        "nuts": "UKI32"
        }
    }
}

Currently I have set it up to get the data by generating classes for the data using Visual Studio's "Paste Special > Paste JSON as Classes" functionality and then using JSON.Net to Deserialize the whole set of data and then use the latitude and longitude from the resulting object to create a new Location object.

Rootobject locationData = JsonConvert.DeserializeObject<Rootobject>(content);

        Location locationToReturn = new Location()
        {
            Latitude = locationData.result.latitude,
            Longitude = locationData.result.longitude
        };

It seems silly to me that I have to go through the effort of Deserializing the whole thing when I only want two of the fields.

So the question is:

Can I deserialize only the latitude and longitude fields?

More specifically to my example, can I deserialize the latitude and longitude fields directly into a new instance of my Location class?


Solution

  • You can load your data into a dynamic object. And then read it from there.

            dynamic locationData = JsonConvert.DeserializeObject<dynamic>(content);
    
            Location locationToReturn = new Location()
            {
                Latitude = locationData.result.latitude,
                Longitude = locationData.result.longitude
            };
    
            Console.WriteLine("Latitude: " + locationToReturn.Latitude);
            Console.WriteLine("Longitude: " + locationToReturn.Longitude);
    
            Console.ReadLine();
    

    This way you can get other columns or values as well w/o enumerating through all the fields.