Get JSON from API and Process that Data

Get JSON to process in your Application

In this knowledge share I would like to show you how you can collect JSON from an external API, and then create C# classes and a Root object class that represent the JSON structure to process the data.

I am using Visual Studio 2022 for this. The first section describes how to set up a console project. The second section shows the code that is needed to create a working example to collect and process JSON data from an API.

Create a new C# Console Application

  1. Open Visual Studio 2022.
  2. Select Create a new project.
    Create new Console Application in Visual Studio
  3. Select Console App and then click Next.
    Select Console App in Visual Studio
  4. In the Project Name field, type MyJsonApiTutorial and then click Next.
    Type in project name for Console App in Visual Studio
  5. In the Framework field, select .NET 6.0 (Long Term Support) and then click Create.
    Select type of .NET framework to be used for the Console App in Visual Studio
  6. Press the F5 key on your keyboard to test if the default application works.
    Default output of Console App
  7. Press the Spacebar key on your keyboard to stop the Console Application.
  8. If not open already, open the file Program.cs in Visual Studio.
  9. Replace the existing code with the following code:
    namespace MyJsonApiTutorial
    {
    internal class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Hello, from MyJsonApiTutorial");
    Console.ReadKey();
    }
    }
    }
  10. Press they F5 key to test if the modified application works.
    Default output of Console App

Get Sample JSON Data to create Classes

  1. In your Internet browser, open the website https://randomuser.me/api/.
  2. Copy the sample JSON that is shown in the Internet brower.

    Note:Your data sample will be different than that shown in the image. New data is created each time you open the web page.

    Sample JSON data
  3. Open the website: https://json2csharp.com/.
  4. Make sure that the button JSON to C# is selected. See image.
  5. Paste the JSON data in the ‘JSON’ column on the left-hand side.
    Open website json2csharp
  6. Click the Convert button.
  7. Copy the content in the right-hand column.
    Class content
  8. in Visual Studio, right-click the project name to select Add > Class.
    Add Class
  9. In the Name field, type User.cs and click Add.
    Add Class dialog
  10. In the User.cs file remove the User class.
    Remove User Class
  11. Paste the Class data in User.cs
    Paste generated Class content in class
  12. Copy the following GetJson() Method to file Program.cs underneath the Main method.
    private static void GetJson()
    {
    using HttpClient client = new HttpClient();
    var response = client.GetAsync("https://randomuser.me/api/").Result;
    UserRoot? userData;
    var jsonString = string.Empty;
    if (response.IsSuccessStatusCode)
    { jsonString = response.Content.ReadAsStringAsync().Result;
    Object? k = JsonConvert.DeserializeObject<object>(jsonString);
    userData = JsonConvert.DeserializeObject<UserRoot>(jsonString);
    foreach (var item in userData.results)
    {
    Console.WriteLine($"The first and last name is {item.name.first} {item.name.last}.");
    Console.WriteLine($"Congratulations! You processed a JSON file.");
    }
    }
    else
    {
    Console.WriteLine($"No Success Status Code.\n{response.StatusCode}");
    Console.ReadKey();
    return;
    }
    }
  13. Put the cursor on the text JsonConvert and press Alt+Enter.
    Fix missing packages
  14. In the context menu select Install package 'Newtonsoft.Json' > Find and install latest version.

    Note:The red squickly underneath JsonConvert should disappear.

  15. Update the Main method with the following content:
    static void Main(string[] args)
    {
    GetJson();
    Console.ReadKey();
    }
  16. Press F5 to test the project.
    Application output

This is the end of this tutorial. I hope you find this useful and wish you good luck coding your projects.

Need a Freelance .NET developer?

If you are in need of a freelance .NET developer, feel free to contact me by email: info {at} schoutentranslation.nl.

Created: 8-11-2023 - Last Update: 8-11-2023 - Views: 3503