Published:

.NET 7.0 + RestSharp - Deserialize Dynamic JSON Response from HTTP Request

Tutorial built with .NET 7.0

This is a quick tutorial on how to send an HTTP request to an API with RestSharp and deserialize the JSON response dynamically without creating a custom class to match the data.

The example sends an HTTP GET request to the Reqres API which is a fake online JSON API used for testing, it includes the route /api/users/1 that returns the following sample data for a single user.

{
  "data": {
    "id": 1,
    "email": "[email protected]",
    "first_name": "George",
    "last_name": "Bluth",
    "avatar": "https://reqres.in/img/faces/1-image.jpg"
  }
}


Deserialize JSON into JsonNode with JsonSerializer

By deserializing the response data into a JsonNode object there is no need to create a custom class to access all the properties. The JsonNode class is in the System.Text.Json.Nodes namespace which comes with the .NET framework.

Properties can be accessed by key using square bracket syntax like a Dictionary object (e.g. data["data"]["id"]). The return type is JsonNode but can be cast to the type you expect (e.g. (string)data["data"]["id"]), and there is no need to cast to string when outputting to the screen.

This is the code to deserialize a json string into a JsonNode object using the JsonSerializer class from System.Text.Json:

var data = JsonSerializer.Deserialize<JsonNode>(jsonString);


Can't deserialize JSON into dynamic with JsonSerializer

Note that the JsonSerializer from System.Text.Json does not support deserializing a JSON string into a dynamic object. So for accessing dynamic JSON properties the simplest approach I've found is to use JsonNode.


RestSharp Request Example with Deserialized Response

The below C# example code uses RestSharp to send a GET request, deserialize the response and write JSON properties out to the console.

Run and edit the code on DotNetFiddle at https://dotnetfiddle.net/RyKyMG.

using RestSharp;
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
    
public class Program
{
    public static void Main()
    {
        // send GET request with RestSharp
        var client = new RestClient("https://reqres.in");
        var request = new RestRequest("api/users/1");
        var response = client.Get(request);
        
        // deserialize json string response to JsonNode object
        var data = JsonSerializer.Deserialize<JsonNode>(response.Content);
        
        Console.WriteLine($"""
        ----------------
        json properties
        ----------------
        id: {data["data"]["id"]}
        email: {data["data"]["email"]}
        first name: {data["data"]["first_name"]}
        last name: {data["data"]["last_name"]}
        avatar: {data["data"]["avatar"]}

        ----------------
        raw json data
        ----------------
        {data}
        """);
    }
}


JSON Properties from RestSharp Request

This is the output from the example code, first the individual json properties from the HTTP response followed by the raw json data object.

----------------
json properties
----------------
id: 1
email: [email protected]
first name: George
last name: Bluth
avatar: https://reqres.in/img/faces/1-image.jpg

----------------
raw json data
----------------
{
  "data": {
    "id": 1,
    "email": "[email protected]",
    "first_name": "George",
    "last_name": "Bluth",
    "avatar": "https://reqres.in/img/faces/1-image.jpg"
  },
  "support": {
    "url": "https://reqres.in/#support-heading",
    "text": "To keep ReqRes free, contributions towards server costs are appreciated!"
  }
}

 


Subscribe or Follow Me For Updates

Subscribe to my YouTube channel or follow me on Twitter, Facebook or GitHub to be notified when I post new content.

Other than coding...

I'm currently attempting to travel around Australia by motorcycle with my wife Tina on a pair of Royal Enfield Himalayans. You can follow our adventures on YouTube, Instagram and Facebook.


Need Some .NET Help?

Search fiverr to find help quickly from experienced .NET developers.



Supported by