How to Parse JSON Data in C# - Coding Gems

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello, this is Jose Luis here at ParametricCamp. And welcome to another video in our coding jam series, where what I would like to teach you is how to serialize and deserialize JSON data using the C sharp programming language. And what do I mean by that? Well, you've probably encountered something like this in your life, this is a file that has some data that is using the JSON structure to represent key value pairs. Turns out that this kind of format is one of the most popular formats for data exchange in the internet, with for HTTP request as data exchange and applications. And it turns out that C sharp is actually pretty good at importing and importing this kind of data. So what I would like to teach you in this video is how to take for example, data that is saved in a file as a JSON file in your system, how to read that file and turn it into an object that you can retrieve particular properties from. And then also how once we have an object that lives in C Sharp, and that where we can manipulate the properties, how to turn that how to turn that object into a JSON representation as a string, in case we need to send it as an HTTP request, or we need to save it as a file, etc, etc. This is going to be perhaps one of the most useful and practical videos that I've done in many, many videos in this channel. But it's going to be definitely super helpful for you as well, because you're going to encounter JSON data in your programming life, for sure to 100% Guaranteed. Okay, let's take a look at how to do this in C Sharp. Alright, so let's start with the basics. What are we going to be doing in this video in this video, I would like to teach you how to D serialize. JSON data. And what is JSON data. JSON stands for JavaScript Object Notation, and is basically a way of organizing data that makes it very easy for machines to read and write. So if you remember from previous videos, where I was showing you how to de serialize and parse CSV data, comma separated values, there's actually a link to the video popping up in the corner of this screen. This is just an alternative way that is perhaps a bit more flexible and less tabulated. So if you ever have a JSON file, or JSON file, or the JSON data structure is going to look something like this, it's going to have some open and closing curlies. And it's going to have basically, a set of key value pairs is going to have the key that is the name of the property. And that is going to be the value of that property, the key is always going to be a string. And the property, or the value is always going to be something of the basic primitive types that are available in C Sharp and in other programming languages. Like for example, a string, or it could be a Boolean, or it could be an integer, or it could be a double, or it can also be other arrays of data, or it can also be other JSON objects themselves. Okay. So it's a bit more, it's a bit richer data structure. And it turns out that JSON data is one of the most popular formats these days for exchanging information, especially if you're working with data that is going into or that is coming from the internet, and from API's application programming interfaces. So knowing how to read JSON data and incorporate it into our C sharp programs, or knowing how to take some data that we have in C Sharp and generating a JSON representation of that data object and sending it somewhere to an API call or whatever turns out to be really, really useful. So what I would like to teach you in this video is how having JSON data how to turn it into a C sharp object, or vice versa. So that will be serializing, or deserializing information. The way we're going to do that is I'm going to start a Visual Studio intense instance. And I'm going to create a very simple project called JSON testing, I'm going to be using the dotnet framework because a lot of the work that I do also integrate into software integrated software that uses the dotnet framework. But you're welcome. If you're a bit more modern, or you're watching this from the future, you're welcome to use the dotnet core dotnet standard, whatever you want to do, okay, so I'm going to create this console application called JSON serialization, see real life session. Okay, I'm going to place it on my desktop, and then I'm going to have this simple console application. Alright. So you've seen how I have this JSON file in my file system, but for the for starters to make it a bit easier. I'm just going to copy paste this JSON string that has basically a data structure representing some kind of person or some kind of YouTuber named Leave me. So there's going to be my name, my channel, whether if I'm doing this or not, etc, etc, okay, and then I'm going to console write this, I'm going to dump it on the console. And I'm going to pause the execution of these console application. And I'm going to see if this works. So I'm going to crank this. And this works. And I'm getting right now a dump of this JSON data structure in the form of a string, and I'm posting it into the console. Okay, beautiful. But as you may or may not have noticed, this is actually a fairly complicated string. It has a lot of like curly brackets, apostrophes, double quotes, square brackets, etc, etc. So cherry picking from this entire data struck, and this is actually a very simple one. But cherry picking from this day entire data structure, the objects that I may need, like my name, or my channel, etc, could be tedious by just doing string manipulation. So that's why there are tools that help us to take something that is in JSON format, and D serialize it into an object that we can cleanly use an exchange and vice versa. Okay. So let me show you what that looks like. At the time of this recording, the dotnet framework doesn't have very streamline or very elegant ways of dealing with JSON files. So one of the most popular tools at the time of this recording for Jason's D serialization is actually this library from Newton soft called JSON dotnet. This is kind of the standard these days that people use. But if you're in the future, and using the.net core, or dotnet, flat dotnet, some of these tools are already embedded in that framer, I will explain that in a second. So the way this works is that we're going to be importing the dot the json.net library into our project. And then we're going to be doing this thing, which is called Creating a schema of the data that we want to D serialize. What that means is that, what we're going to be doing is basically create a class that represents a template for the data that we expect to deserialize. And hopefully, the data that we have, will match that template for an object and then designing that template and then using the JSON convert class of the library to deserialize. This object will make it very, very simple. Okay, so the first thing we're going to do is, because we are in the dotnet framework, we are going to bring in the Newton soft dot JSON library for the serialization. The way I'm going to do that is I'm going to right click on my project, I'm going to go to Manage nougat packages, I'm going to go to Browse, and here I'm going to type new Tom soft. And you can see that the first thing that pops up is this library that has 3.2 billion downloads, which means that it's kind of popular, I'd still these days, right, so I'm going to install this nougat package in my project correct. And then I'm going to go to the beginning, and I'm going to say using Newton soft dot j s, alright. Now, if you're watching this from the future, and you're using dotnet, or dotnet standard, or you're just simply not into dotnet framework, then my understanding is that you might be able to use system dot text dot JSON, which is available in non dotnet frameworks, okay? For non dotnet framework. However, because I am in dotnet framework, that's where we are this video, we're going to be using the Newton soft version, but I believe that actually both are almost almost identical. And then you could be switching between the two with no problem. All right. So with this, you're going to see how easy this is going to be. So I have this string representation of an object that is something that has a name, a channel, active age, and some members. So I think this may be I could basically represent it with some kind of template that I'm going to call YouTube. Alright. And so what I'm going to do is I'm going to define an internal class that is going to live in parallel here with me. So I'm going to say here, I'm going to save public class and I'm going to define a class that is going to be the template that all this information is going to adhere to. I'm going to call this YouTuber, for example, open and close curly. And what I would like to do now is I would like to define properties with their data types that match what I expect from my JSON data. So for example, this is going to be For example, a string called name, and I need to be very careful so that this property name matches the property that I expect in, in the JSON file exactly, including the casing, I believe, right. So for example, here, I would like to say public string is going to be channel also, it needs to match real well, then for active, this is going to be a public, public Boolean, and then I'm going to call active public and for the age is probably going to be an integer, because, you know, sorry, because I will never define HS 2.3. That's not kind of a thing. And then, for our members, because this is going to be an array of strings, something that I can do is I can say, well, I'm going to define this as a list of strings. I have a list of string, I'm going this going to call members, you probably also could use an array. But lists are just very handy in in C Sharp. And then once I have defined the template for the data structure that is going to come from this JSON object, then it's just going to be really, really easy. So I'm going to remove this. And I'm going to say I'm going to declare a variable called the serialized. And that's going to be and you see, autocomplete, Visual Studio is much smarter than me. So the autocomplete already gave me everything that I need to do. So JSON convert is basically a static class that leaves as part of the Newton soft package. Alright, and I'm going to write this manually. And then from that, I can just ask for the serialization method. So can I be serialized a particular object, and what I can do is I can force that object to be of a particular type. So because I have already fine YouTuber, I can say, can you deserialize, the object that I'm going to give you make sure that is going to be of the type of the template that I generated YouTuber, and then in parentheses, I want to give it this stream that has that JSON data. So that's going to be the JSON that I got from there. Alright. So I did declare this as an implicit variable with bar, but technically, the return of this object is going to be something that is going to be of the type YouTuber, so it's an variable of this type. So hopefully, after this works, so what I can do is I can start printing things. So for example, the serialized name, I can print out the serialized channel, the serialized, active and the serialized dot age. And then for each for each variable member in this, you can see that the serialized dot members, I can also print member, member, member, and then member, all right. So if I run this, hopefully, this is going to work. And you can see that indeed, from my object, I'm already printing, the name, the channel, the active the h, and then each one of the members that are part of our beautiful ParametricCamp community. Okay, fantastic. So this would be an example of D serialization, or from a string representation of a JSON file, how to turn it into a C sharp, fully fledged object. Alright. Now, what if I wanted to take this YouTuber, and I needed to send it as a JSON file somewhere to an API request, etc? How would I do that? The process now that we have the entire thing is actually super, super simple. Look at this, the only thing that I need to do now is I need to say, I'm going to create a string variable, because Jason's are basically strings at the end of the day that I'm going to call C re Leist. All right. And what I'm going to do is basically the inverse operation, I'm going to use JSON convert. And here what I'm going to instead of deserializing, I'm going to serialize an object. In this case, I don't need to specify the type because as soon as I give it the object itself, which is going to be the serialized, right, then JSON is going to know what to do with this. And after I do that, then what I can do is I can just print the entire JSON serialized object alright. So for example, like this, you can see that if I do this now I have what I printed before and now I have a full fledge first class citizen json file with the name channel active, etc, etc. No All the information. So this is going to be super, super handy if you ever need to do any JSON serialization or D serialization. Now here for the string, we actually did take this string from, we actually copy pasted the string right over here. But this is also something that we could read from a file. So I can just say here, string that JSON. And can I read the information that is inside a JSON file somewhere in my system, and in my system, I have this file here. So for example, this is going to be system dot I, oh, read all the text. And then here, what I would like to do is I would like to input where my file is. So that's going to be somewhere here in my system. So I can right click with and copy as a path here. And then I can just say, with the, with the ad, so that I don't have to escape all these characters. See user's desktop parameter, Kendall Jackson. And then hopefully, this should also work, correct? Yes, it does work. And now the information is being read by the by from the file. Okay. Beautiful. So if you're working with data, JSON files, if you're doing HTTP POST request, GET request, etc, etc. From C sharp, all of these is going to be extremely, extremely useful for you. All right. So hopefully this was useful. Thank you very much for watching this video. If you liked what you saw, please subscribe to the channel, watch our other videos, say hi, join our Discord, etc, etc. Okay, thanks a lot. Bye bye.
Info
Channel: ParametricCamp
Views: 15,671
Rating: undefined out of 5
Keywords: C#, Visual Studio, Computer Programming, Computational Design, Design, JSON, Parsing, Serialization
Id: Y14gG9IJ230
Channel Id: undefined
Length: 16min 51sec (1011 seconds)
Published: Mon Jun 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.