0

First time working with JSON in C#. I'm building a Blazor app using Amadeus API data (flights search) and I cannot seem to be able to use the JSON object in a Blazor component.

The error I'm getting:

Unhandled exception rendering component: The JSON value could not be converted to System.Collections.Generic.List`1[Flights.Models.RootObject]

which I think might be because I'm not building my C# classes correctly.

Here's a sample of the JSON object, not all but with the needed information:

{
  "data": [
    {
      "type": "flight-destination",
      "origin": "XOC",
      "destination": "YJB",
      "departureDate": "2025-08-19",
      "returnDate": "2025-08-21",
      "price": {
        "total": "60.0"
      },
      "links": {
        "flightDates": "https://test.api.amadeus.com/v1/shopping/flight-dates?origin=XOC&destination=YJB&departureDate=2025-08-19,2025-08-21&oneWay=false&nonStop=false&viewBy=DESTINATION",
        "flightOffers": "https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=XOC&destinationLocationCode=YJB&departureDate=2025-08-19&returnDate=2025-08-21&adults=1&nonStop=false&viewBy=DESTINATION"
      }
    },
    {
      "type": "flight-destination",
      "origin": "MAD",
      "destination": "AUH",
      "departureDate": "2025-07-22",
      "returnDate": "2025-07-28",
      "price": {
        "total": "679.07"
      },
      "links": {
        "flightDates": "https://test.api.amadeus.com/v1/shopping/flight-dates?origin=MAD&destination=AUH&departureDate=2025-07-22,2025-07-28&oneWay=false&nonStop=false&viewBy=DESTINATION",
        "flightOffers": "https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=MAD&destinationLocationCode=AUH&departureDate=2025-07-22&returnDate=2025-07-28&adults=1&nonStop=false&viewBy=DESTINATION"
      }
    }
  ],
  "dictionaries": {
    "currencies": {
      "EUR": "Euro"
    },
    "locations": {
      "MXP": {
        "subType": "AIRPORT",
        "detailedName": "MALPENSA"
      },
      "AMS": {
        "subType": "AIRPORT",
        "detailedName": "SCHIPHOL AIRPORT"
      }
    }
  },
  "meta": {
    "currency": "EUR",
    "links": {
      "self": "https://test.api.amadeus.com/v1/shopping/flight-destinations?origin=MAD&departureDate=2025-07-16,2026-01-10&oneWay=false&nonStop=false&viewBy=DESTINATION"
    },
    "defaults": {
      "departureDate": "2025-07-16,2026-01-10",
      "oneWay": false,
      "duration": "1,15",
      "nonStop": false,
      "viewBy": "DESTINATION"
    }
  },
  "warnings": []
}

I then tried to mirror my C# classes to match the JSON structure:

namespace Flights.Models
{
    public class RootObject
    {
        public Data[] data { get; set; }
        public Dictionaries dictionaries { get; set; }
        public Meta meta { get; set; }
        public object[] warnings { get; set; }
    }

    public class Data
    {
        public string type { get; set; }
        public string origin { get; set; }
        public string destination { get; set; }
        public string departureDate { get; set; }
        public string returnDate { get; set; }
        public Price price { get; set; }
        public Links links { get; set; }
    }

    public class Price
    {
        public string total { get; set; }
    }

    public class Links
    {
        public string flightDates { get; set; }
        public string flightOffers { get; set; }
    }

    public class Dictionaries
    {
        public Currencies currencies { get; set; }
    }

    public class Currencies
    {
        public string EUR { get; set; }
    }

    public class Meta
    {
        public string currency { get; set; }
        public Links1 links { get; set; }
        public Defaults defaults { get; set; }
    }

    public class Links1
    {
        public string self { get; set; }
    }

    public class Defaults
    {
        public string departureDate { get; set; }
        public bool oneWay { get; set; }
        public string duration { get; set; }
        public bool nonStop { get; set; }
        public string viewBy { get; set; }
    }
}

And the method I'm using to get the data into the destinations field so I can pass this one into a children component:

private List<RootObject> destinations;

private async Task<List<RootObject>?> GetMostTravelledDestinations(string accessToken)
{
    var request = new HttpRequestMessage(HttpMethod.Get, "https://test.api.amadeus.com/v1/shopping/flight-destinations?origin=MAD");
    request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
    request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

    var response = await Http.SendAsync(request);

    if (response.IsSuccessStatusCode)
    {
        var json = await response.Content.ReadAsStringAsync();
        var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
        var parsed = JsonSerializer.Deserialize<List<RootObject>>(json, options);
            
        if (parsed != null)
        {
            foreach (var rootObject in parsed)
            {
                Console.WriteLine($"{rootObject.data}");
            }

            destinations = parsed;

            using var doc = JsonDocument.Parse(json);
            var root = doc.RootElement;

            if (root.TryGetProperty("data", out var dataArray))
            {
                Console.WriteLine($"Data contains {dataArray.GetArrayLength()} items.");
            }
            else
            {
                Console.WriteLine("No 'data' property found.");
            }

            return parsed;
        }
    }

    return new List<RootObject>();
}

2 Answers 2

1

The toplevel Json is not a list.

//  var parsed = JsonSerializer.Deserialize<List<RootObject>>(json, options);
    var parsed = JsonSerializer.Deserialize<RootObject>(json, options);
1
  • Thanks. I answered in the below answer too.
    – Fab
    Commented 19 hours ago
0

In your json you have List RootObject.data but RootBoject is not a list.

Change

var parsed = JsonSerializer.Deserialize<List<RootObject>>(json, options);

To

var parsed = JsonSerializer.Deserialize<RootBoject>(json, options);

And use it like this:

parsed.Data
1
  • Oh my bad, you're right. I was having trouble understanding how it worked in C# and didn't noticed I left a List in there from previous code. It's working now though, thank you.
    – Fab
    Commented 20 hours ago

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.