Parsing JSON in C#


A lot of APIs will give you responses in JSON format. Here we'll review JSON parsing in C# so that you can get to the interesting data faster.

This tutorial assumes that you've already gone through our C# getting started tutorial and are familiar with how our C# SDK works. Although we use the output from our YouTube ListSearchResults Choreo in this tutorial, the same steps we outline here will work for parsing any JSON in C#.

Get JSON output

1 Log in to Temboo and go to the YouTube > Search > ListSearchResults Choreo in our Library.

2 Enter any search term you want for the Query input and click Generate Code to test the Choreo from our website.

3 You get a whole bunch of JSON in the Response output. These are the results of the search. Next we'll see how to parse through this response in C# and pick out only the pieces we're interested in.

Parse it in C#

By default, C# encourages you to create concrete classes to use when parsing JSON data, as described in this article. However, when you want to experiment with data from multiple APIs, a more lightweight alternative is to use a generic third-party parsing library. In this tutorial we'll walk through installing a popular C# JSON parser and using it to work with the Youtube data.

4 Create a new Visual Studio solution containing a "C# Console Application" project.

5Make sure you've downloaded the Temboo C# SDK, and added TembooSDK.dll as a reference to your "C# Console Application" project as described in our getting started tutorial.

6 Download the JSON.Net library and unzip the file. Move Newtonsoft.Json.dll into the directory where you keep your Visual Studio projects.

7 Under your "C# Console Application" project, right-click References and select Add Reference. In the dialogue box that appears, select Browse and select Newtonsoft.Json.dll. Click OK.

8 Replace the contents of Program.cs with the code below. In steps 9-11, we'll go over what happens in the code.

using System;
using Newtonsoft.Json.Linq;
using Temboo.Core;
using Temboo.Library.Youtube.Search;

namespace ConsoleApplication1
{
    class ParsingJSON
    {
        static void Main(string[] args)
        {
            // Instantiate a TembooSession object using your Account name and Application key
            TembooSession session = new TembooSession("ACCOUNT_NAME", "APP_NAME", "APP_KEY");

            // Instantiate the Choreo using the TembooSession
            ListSearchResults listSearchResultsChoreo = new ListSearchResults(session);

            // Set inputs
            listSearchResultsChoreo.setQuery("Quantum entanglement");

            // Execute Choreo
            ListSearchResultsResultSet listSearchResults = listSearchResultsChoreo.execute();

            // Parse the results from Youtube into a JSON object
            JObject json = JObject.Parse(listSearchResults.Response);
  
            // Retrieve the "snippet" element for the first entry in the "items" array
            JObject firstItemSnippet = JObject.Parse(json["items"][0]["snippet"].ToString());

            // Print the title and description for the snippet
            Console.WriteLine(firstItemSnippet["title"]);
            Console.WriteLine(firstItemSnippet["description"]);

            // Wait for input, to prevent the Terminal from exiting immediately
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}

9First we converted the JSON text from the response to a C# Object using the JObject.Parse method.

10 Next, we parsed out the piece of data we want from the JSON. It helps to look at the JSON file's structure to get an idea of how it is organized. The two main elements you should look for are:

What it meansHow it appears in the JSON file
Array of JSON Elements"name": [
Specific JSON Element"name": {

For these results, we wanted the title of the first video in the search results. We got the items array, then the first item in that array (at index 0). Then we wanted the snippet object within the first item in the array. We used the statement json["items"][0]["snippet"] to grab this specific element within the JSON data.

11 To finish up, we parsed the title property from the snippet object using the firstItemSnippet["title"] accessor.

12All finished! Run the code in Visual Studio to try it out. You should see the title of your first YouTube Search result in the console.

What next?

Now you should be to able to parse all sorts of JSON responses with our Java SDK. Check out the 2000+ Choreos in our Library and find some exciting data to parse.

Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.

Need help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.

We're hiring!

Like what we do? Take a look at our open positions.


Back