{"id":83203,"date":"2023-03-03T07:00:34","date_gmt":"2023-03-03T06:00:34","guid":{"rendered":"https:\/\/code-maze.com\/?p=83203"},"modified":"2024-04-04T17:50:06","modified_gmt":"2024-04-04T15:50:06","slug":"csharp-read-and-process-json-file","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/","title":{"rendered":"How to Read and Parse a JSON File in C#"},"content":{"rendered":"<p>When we develop software applications, we use JSON (JavaScript Object Notation) as a data interchange format, therefore we need to understand how to read and parse JSON files.<\/p>\n<p>In this article, we are going to explore six distinct ways to read and parse a JSON file in C#, providing examples to illustrate how to use them effectively. We will parse the JSON string using either the <code>Newtonsoft.Json<\/code> library or the <code>System.Text.Json<\/code> namespace in each method, featuring a unique combination of reading and parsing tools.<\/p>\n<div style=\"padding: 20px; border-left: 5px color:#dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for the video, visit our <a href=\"https:\/\/www.patreon.com\/posts\/source-code-6-to-99785607?src=readParseJsonFile\" target=\"_blank\" rel=\"nofollow noopener\">Patreon page<\/a> (YouTube Patron tier).<\/div>\n<p>At the end of the article, we will compare the performance of these methods using the <code>BenchmarkDotNet<\/code> library.<\/p>\n<hr \/>\r\n<p style=\"text-align: center;\"><strong>VIDEO<\/strong>: How to Read and Parse a JSON File in C# video.<\/p>\r\n<p style=\"text-align: center;\"><iframe width=\"560\" height=\"315\" src=https:\/\/www.youtube.com\/embed\/PM1pIprC7Go?si=GBqaq9omsyTyMO78 frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\r\n<hr \/>\n<p>Let&#8217;s dive in.<\/p>\n<h2><a id=\"H2\"><\/a>Prepare the Environment<\/h2>\n<p>Before we start discussing the various methods we can use to read and parse JSON files in .NET, we need to define a sample JSON file and populate it with data:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\">[\r\n    {\r\n        \"teacherId\":1,\r\n        \"firstName\":\"Clare\",\r\n        \"lastName\":\"Anyanwu\",\r\n        \"birthYear\":1987,\r\n        \"level\":8,\r\n        \"courses\":\r\n        [\r\n            {\r\n                \"name\":\"Biology\",\r\n                \"creditUnits\":3,\r\n                \"numberOfStudents\":42\r\n            },\r\n            {\r\n                \"name\":\"Basic Science\",\r\n                \"creditUnits\":4,\r\n                \"numberOfStudents\":35\r\n            }\r\n        ]\r\n    }\r\n]<\/pre>\n<p>This JSON file will be the input data for all the methods in this article.<\/p>\n<h2><a id=\"H2\"><\/a>Read and Parse JSON File Using Newtonsoft.Json<\/h2>\n<p><code>Newtonsoft.Json<\/code> or <code>JSON.NET<\/code> is a popular, open-source library for reading and parsing JSON data in .NET. In this section, let&#8217;s look at some of the different ways we can work with the <code>JSON.NET<\/code> library to read and parse a JSON file in C#.<\/p>\n<h3><a id=\"H3\"><\/a>Read and Parse a JSON File Into a .NET Object With Newtonsoft.Json<\/h3>\n<p>To read and parse a JSON file into a .NET object with <code>Newtonsoft.Json<\/code>, we can use the <code>JsonConvert.DeserializeObject()<\/code> method, which is a part of the <code>Newtonsoft.Json<\/code> library.<\/p>\n<p>First, we define the <code>Teacher<\/code> class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Teacher\r\n{\r\n    public int TeacherId { get; set; }\r\n    public string FirstName { get; set; } = string.Empty;\r\n    public string LastName { get; set; } = string.Empty;\r\n    public int BirthYear { get; set; }\r\n    public int Level { get; set; }\r\n    public List&lt;Course&gt; Courses { get; set; }\r\n}<\/pre>\n<p>This class contains properties that correspond to the data in the JSON file. The <code>Courses<\/code> property is a list of the type <code>Course<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Course\r\n{\r\n    public string Name { get; set; } = string.Empty;\r\n    public int CreditUnits { get; set; }\r\n    public int NumberOfStudents { get; set; }\r\n}<\/pre>\n<p>Now, let&#8217;s create a\u00a0<code>ReadAndParseJsonFileWithNewtonsoftJson<\/code> class and a <code>_sampleJsonFilePath<\/code> variable:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class ReadAndParseJsonFileWithNewtonsoftJson\r\n{\r\n    private readonly string _sampleJsonFilePath;\r\n\r\n    public ReadAndParseJsonFileWithNewtonsoftJson(string sampleJsonFilePath)\r\n    {\r\n        _sampleJsonFilePath = sampleJsonFilePath;\r\n    }\r\n}<\/pre>\n<p>Here, we define a class that will contain all our methods that will use the <code>Newtonsoft.Json<\/code> library to parse our JSON data. We also define a <code>private<\/code> <code>readonly<\/code> string that points to our sample JSON file path.<\/p>\n<p>Then, we define a constructor that initializes <code>_sampleJsonFilePath<\/code> with the value of the <code>sampleJsonFilePath<\/code> parameter. Classes that call our JSON file reading and parsing methods will set the value of this parameter.<\/p>\n<p>Now, let&#8217;s define a <code>UseUserDefinedObjectWithNewtonsoftJson()<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public List&lt;Teacher&gt; UseUserDefinedObjectWithNewtonsoftJson()\r\n{\r\n    using StreamReader reader = new(_sampleJsonFilePath);\r\n    var json = reader.ReadToEnd();\r\n    List&lt;Teacher&gt; teachers = JsonConvert.DeserializeObject&lt;List&lt;Teacher&gt;&gt;(json);\r\n\r\n    return teachers;\r\n}<\/pre>\n<p>Here, \u00a0we\u00a0create a new <code>StreamReader<\/code> object by passing the path of our JSON file to the constructor. The using statement ensures that the <code>StreamReader<\/code> is disposed of properly when we&#8217;re done with it.<\/p>\n<p>After that, we call the <code>JsonConvert.DeserializeObject()<\/code> method, passing in the JSON string and the <code>Teacher<\/code> class as the generic type parameter.<\/p>\n<p>Finally, we return a list of <code>Teacher<\/code> objects deserialized from the JSON data.<\/p>\n<p>We can see that using the <code>StreamReader<\/code> class and the <code>Newtonsoft.Json<\/code> library to read and parse JSON data into .NET objects is quite simple.<\/p>\n<h3><a id=\"H3\"><\/a>Read and Parse a JSON File Using JsonTextReader in Newtonsoft.Json<\/h3>\n<p>Now, let&#8217;s see how we can read and parse a JSON file using the <code>JsonTextReader<\/code> with the <code>Newtonsoft.Json<\/code> library in C#.<\/p>\n<p>First, let&#8217;s define a <code>UseJsonTextReaderWithNewtonsoftJson()<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public List&lt;Teacher&gt; UseJsonTextReaderInNewtonsoftJson()\r\n{\r\n    var serializer = new JsonSerializer();\r\n    List&lt;Teacher&gt; teachers = new();\r\n    using (var streamReader = new StreamReader(_sampleJsonFilePath))\r\n    using (var textReader = new JsonTextReader(streamReader))\r\n    {\r\n        teachers = serializer.Deserialize&lt;List&lt;Teacher&gt;&gt;(textReader);\r\n    }\r\n\r\n    return teachers;\r\n}<\/pre>\n<p>Here, we create a <code>JsonSerializer<\/code> object to deserialize the JSON data. Then, we also create an empty List of type <code>Teacher<\/code> to store the deserialized data.<\/p>\n<p>Next, we define a <code>StreamReader<\/code> object to read the contents of the JSON file, and then use a <code>JsonTextReader<\/code> to read the JSON data from the stream reader.<\/p>\n<p>Finally, we call the <code>Deserialize()<\/code> method of the <code>JsonSerializer<\/code> object, passing in the <code>JsonTextReader<\/code>. We invoke this method to convert the JSON data into a List of <code>Teacher<\/code> objects and return the list.<\/p>\n<h3><a id=\"H3\"><\/a>Read and Parse JSON File Using JArray.Parse() in Newtonsoft.Json<\/h3>\n<p>Let&#8217;s see how we can use the <code>JArray.Parse()<\/code> method in <code>Newtonsoft.Json<\/code> to read and parse a JSON file. To demonstrate this, let&#8217;s define a <code>UseJArrayParseWithNewtonsoftJson()<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public List&lt;Teacher&gt; UseJArrayParseInNewtonsoftJson()\r\n{\r\n    using StreamReader reader = new(_sampleJsonFilePath);\r\n    var json = reader.ReadToEnd();\r\n    var jarray = JArray.Parse(json);\r\n    List&lt;Teacher&gt; teachers = new();\r\n\r\n    foreach (var item in jarray)\r\n    {\r\n        Teacher teacher = item.ToObject&lt;Teacher&gt;();\r\n        teachers.Add(teacher);\r\n    }\r\n\r\n    return teachers;\r\n}<\/pre>\n<p>First, we use a <code>StreamReader<\/code> object to read the contents of the JSON file into a string variable called <code>json<\/code>.<\/p>\n<p>Next, we invoke the\u00a0<code>JArray.Parse()<\/code> method and pass the JSON string to it. This method parses the string into a <code>JArray<\/code> object, which is a collection of <code>JToken<\/code> objects representing the data in the JSON file.<\/p>\n<p>We then create an empty list of <code>Teacher<\/code> objects called <code>teachers<\/code>.<\/p>\n<p>We use a <code>foreach<\/code> loop to iterate through each <code>JToken<\/code> in the <code>JArray<\/code>. For each <code>JToken<\/code>, we call the <code>ToObject&lt;Teacher&gt;()<\/code> method to create a <code>Teacher<\/code> object using the properties of the <code>JToken<\/code>.<\/p>\n<p>After that, we add the newly created <code>Teacher<\/code> object to the <code>teachers<\/code> list.<\/p>\n<p>Finally, we return the list of teachers.<\/p>\n<h2><a id=\"H2\"><\/a>Read and Parse JSON File Using System.Text.Json<\/h2>\n<p>In this section, let&#8217;s discuss various methods that we can use to read and parse a JSON file in C# using the native <code>System.Text.Json<\/code> namespace.<\/p>\n<p>First, let&#8217;s define a <code>ReadAndParseJsonFileWithSystemTextJson<\/code> class and a <code>_sampleJsonFilePath<\/code> variable:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class ReadAndParseJsonFileWithSystemTextJson\r\n{\r\n    private readonly string _sampleJsonFilePath;\r\n\r\n    public ReadAndParseJsonFileWithSystemTextJson(string sampleJsonFilePath)\r\n    {\r\n        _sampleJsonFilePath = sampleJsonFilePath;\r\n    }\r\n}<\/pre>\n<p>Unlike the <code>Newtonsoft.Json<\/code> library, the <code>System.Text.Json<\/code> library does not have case-insensitive property name matching built-in. As a result, we need to include a mechanism to enable this functionality.<\/p>\n<p>To allow for case-insensitive property name matching during JSON deserialization, let&#8217;s define an instance of the <code>JsonSerializerOptions<\/code> class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">private readonly JsonSerializerOptions _options = new()\r\n{\r\n    PropertyNameCaseInsensitive = true\r\n};<\/pre>\n<p>When we pass the <code>_options<\/code> parameter to the <code>JsonSerializer.Deserialize()<\/code> method, the deserializer will be able to match properties regardless of their casing.<\/p>\n<p>This can be useful in scenarios where our JSON file may come from different sources with different casing conventions.<\/p>\n<p>Now, let&#8217;s explore the various methods for reading and parsing a JSON file.<\/p>\n<h3><a id=\"H3\"><\/a>Read and Parse JSON File Using File.ReadAllText() With System.Text.Json<\/h3>\n<p>First, let&#8217;s learn how to read and parse a JSON file using the <code>File.ReadAllText()<\/code> method in conjunction with the <code>System.Text.Json<\/code>\u00a0library.<\/p>\n<p>Let&#8217;s take a look at the <code>UseFileReadAllTextWithSystemTextJson()<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public List&lt;Teacher&gt; UseFileReadAllTextWithSystemTextJson()\r\n{\r\n    var json = File.ReadAllText(_sampleJsonFilePath);\r\n    List&lt;Teacher&gt; teachers = JsonSerializer.Deserialize&lt;List&lt;Teacher&gt;&gt;(json, _options);\r\n\r\n    return teachers;\r\n}<\/pre>\n<p>Here, we use the <code>File.ReadAllText()<\/code> method to read the contents of the specified file into a string variable called <code>json<\/code>. This method returns the entire contents of the file as a single string.<\/p>\n<p>Next, we use the <code>JsonSerializer<\/code> class to deserialize the <code>json<\/code> string into a list of <code>Teacher<\/code> objects. We do this by invoking the <code>JsonSerializer.Deserialize&lt;List&lt;Teacher&gt;&gt;()<\/code> method, which takes in the JSON string and a type argument (in this case, <code>List&lt;Teacher&gt;<\/code>) and we return an object of that type.<\/p>\n<p>Finally, we return the deserialized list of <code>Teacher<\/code> objects.<\/p>\n<p>As we can see, using the <code>File.ReadAllText()<\/code> method in combination with the <code>JsonSerializer<\/code> class allows us to quickly and easily convert JSON data into strongly-typed objects.<\/p>\n<h3><a id=\"H3\"><\/a>Read and Parse JSON File Using File.OpenRead() With System.Text.Json<\/h3>\n<p>Alternatively, we can use the <code>File.OpenRead()<\/code> method with <code>System.Text.Json<\/code> to read and parse JSON files in our C# applications.<\/p>\n<p>To start, let&#8217;s define a\u00a0 <code>UseFileOpenReadTextWithSystemTextJson()<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public List&lt;Teacher&gt; UseFileOpenReadTextWithSystemTextJson()\r\n{\r\n    using FileStream json = File.OpenRead(_sampleJsonFilePath);\r\n    List&lt;Teacher&gt; teachers = JsonSerializer.Deserialize&lt;List&lt;Teacher&gt;&gt;(json, _options);\r\n\r\n    return teachers;\r\n}<\/pre>\n<p>Here, we create a new <code>FileStream<\/code> object by calling <code>File.OpenRead()<\/code> and pass it the path of our JSON file. This method opens the file in read-only mode and returns an object, that we store in a variable named <code>json<\/code>.<\/p>\n<p>We then call the static method <code>JsonSerializer.Deserialize()<\/code> to convert the <code>FileStream<\/code> object into a list of <code>Teacher<\/code> instances.<\/p>\n<p>Finally, we return the list of teachers.<\/p>\n<h3><a id=\"H3\"><\/a>Read and Parse JSON File Using StreamReader With System.Text.Json<\/h3>\n<p>We can also use the <code>StreamReader<\/code> class to read a JSON file, and then use the <code>System.Text.Json<\/code> library to parse the returned JSON string.<\/p>\n<p>To see how this works, let&#8217;s create a <code>UseStreamReaderWithSystemTextJson()<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public List&lt;Teacher&gt; UseStreamReaderWithSystemTextJson()\r\n{\r\n    using StreamReader streamReader = new(_sampleJsonFilePath);\r\n    var json = streamReader.ReadToEnd();\r\n    List&lt;Teacher&gt; teachers = JsonSerializer.Deserialize&lt;List&lt;Teacher&gt;&gt;(json, _options);\r\n\r\n    return teachers;\r\n}<\/pre>\n<p>In this method, we create a new instance of <code>StreamReader<\/code> and pass it the path of our JSON file.<\/p>\n<p>We then call the <code>ReadToEnd()<\/code> method of the <code>StreamReader<\/code>\u00a0object to read the entire contents of the JSON file as a string, which we store in a variable called <code>json<\/code>.<\/p>\n<p>Next, we invoke the static method <code>JsonSerializer.Deserialize()<\/code> to convert the JSON data in the string into a list of <code>Teacher<\/code> objects.<\/p>\n<p>Finally, we return the list of teachers.<\/p>\n<h2><a id=\"H2\"><\/a>Benchmark All Methods<\/h2>\n<p>Now that all our methods are ready, let&#8217;s compare their time and memory performance using the <code>BenchmarkDotNet<\/code> library.<\/p>\n<p>To see the benchmark implementation, you can look at\u00a0<a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/blob\/main\/json-csharp\/ReadAndParseAJSONFileInCSharp\/ReadAndParseAJSONFileInCSharp\/ReadAndParseJSONFileMethodsBenchMark.cs\" target=\"_blank\" rel=\"noopener\">this file<\/a> in our repository.<\/p>\n<p>Now, let&#8217;s run our benchmark tests and inspect the results on the console:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">|                                 Method |      Mean |     Error |    StdDev |    Median | Allocated |\r\n|--------------------------------------- |----------:|----------:|----------:|----------:|----------:|\r\n|  UseFileOpenReadTextWithSystemTextJson |  90.94 ms |  2.632 ms |  7.762 ms |  94.99 ms |   4.22 MB |\r\n|      UseStreamReaderWithSystemTextJson | 120.05 ms |  3.551 ms | 10.470 ms | 125.52 ms |   23.1 MB |\r\n|   UseFileReadAllTextWithSystemTextJson | 120.70 ms |  3.407 ms | 10.047 ms | 125.36 ms |  23.09 MB |\r\n|      UseJsonTextReaderInNewtonsoftJson | 179.27 ms |  6.153 ms | 18.047 ms | 185.46 ms |   5.14 MB |\r\n| UseUserDefinedObjectWithNewtonsoftJson | 182.26 ms |  5.289 ms | 15.593 ms | 189.16 ms |  20.25 MB |\r\n|         UseJArrayParseInNewtonsoftJson | 709.25 ms | 19.144 ms | 56.447 ms | 729.78 ms |  77.72 MB |<\/pre>\n<p>Based on the benchmark test results, the fastest method to read and parse a JSON file in C# is the <code>UseFileOpenReadTextWithSystemTextJson()<\/code> method, with an average time of 90.94 ms. This method also utilizes the least amount of memory among all the tested methods, with 4.22 MB allocated.<\/p>\n<p>As we can see, <code>UseStreamReaderWithSystemTextJson()<\/code> and <code>UseFileReadAllTextWithSystemTextJson()<\/code> methods are slightly slower than the <code>UseFileOpenReadTextWithSystemTextJson()<\/code> method, with average times of 120.05 ms and 120.70 ms, respectively. However, they allocate significantly more memory, with 23.1 MB and 23.09 MB allocated, respectively.<\/p>\n<p>Also, the <code>UseJsonTextReaderInNewtonsoftJson()<\/code> method and the <code>UseUserDefinedObjectWithNewtonsoftJson()<\/code> method are both slower than the <code>System.Text.Json<\/code>-based methods, with average times of 179.27 ms and 182.26 ms, respectively. However, the <code>UseJsonTextReaderInNewtonsoftJson()<\/code> method consumes much less memory than the <code>System.Text.Json<\/code>-based methods, with 5.14 MB allocated. While the <code>UseUserDefinedObjectWithNewtonsoftJson()<\/code> method consumes almost the same amount of memory as the <code>System.Text.Json<\/code>-based methods with 20.25 MB allocated.<\/p>\n<p>The slowest method for this benchmark test is the <code>UseJArrayParseInNewtonsoftJson()<\/code> method, with an average time of 709.25 ms. This method also allocates the most memory among all the tested methods, with 77.72 MB allocated.<\/p>\n<p>Overall, we can see that the <code>UseFileOpenReadTextWithSystemTextJson()<\/code> method is the best choice to read and parse a JSON file in C#.<\/p>\n<h2><a id=\"H2\"><\/a>Conclusion<\/h2>\n<p>In this article, we explored six different methods that we can use to read and parse a JSON file in C# with examples. Additionally, we utilized the <code>BenchmarkDotNet<\/code> library to compare the performance of these methods. This article provides a comprehensive guide for developers to understand how to read and parse JSON files in C# and choose the best approach for their use case.<\/p>\n<p>To learn how to perform the reverse process, that is, writing a JSON string to a file, please visit <a href=\"https:\/\/code-maze.com\/csharp-write-json-into-a-file\/\" target=\"_blank\" rel=\"noopener\">How to Write JSON Into a File in C#<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we develop software applications, we use JSON (JavaScript Object Notation) as a data interchange format, therefore we need to understand how to read and parse JSON files. In this article, we are going to explore six distinct ways to read and parse a JSON file in C#, providing examples to illustrate how to use [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12,1369],"tags":[927,1645,1157,1158],"class_list":["post-83203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","category-json","tag-json","tag-json-net","tag-newtonsoft-json","tag-system-text-json","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Read and Parse a JSON File in C#<\/title>\n<meta name=\"description\" content=\"In this article, we are going to discuss six different methods that we can use to read and parse a JSON file in C#.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Read and Parse a JSON File in C#\" \/>\n<meta property=\"og:description\" content=\"In this article, we are going to discuss six different methods that we can use to read and parse a JSON file in C#.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-03T06:00:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-04T15:50:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Januarius Njoku\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/cjaynjoku\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Januarius Njoku\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/\"},\"author\":{\"name\":\"Januarius Njoku\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/c9d04eed48c29b9b10e3a0ec0fa77a18\"},\"headline\":\"How to Read and Parse a JSON File in C#\",\"datePublished\":\"2023-03-03T06:00:34+00:00\",\"dateModified\":\"2024-04-04T15:50:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/\"},\"wordCount\":1549,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"JSON\",\"Json.NET\",\"Newtonsoft.Json\",\"System.Text.Json\"],\"articleSection\":[\"C#\",\"JSON\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/\",\"url\":\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/\",\"name\":\"How to Read and Parse a JSON File in C#\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2023-03-03T06:00:34+00:00\",\"dateModified\":\"2024-04-04T15:50:06+00:00\",\"description\":\"In this article, we are going to discuss six different methods that we can use to read and parse a JSON file in C#.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Read and Parse a JSON File in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/c9d04eed48c29b9b10e3a0ec0fa77a18\",\"name\":\"Januarius Njoku\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/01\/Januarius-Njoku-150x150.jpeg\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/01\/Januarius-Njoku-150x150.jpeg\",\"caption\":\"Januarius Njoku\"},\"description\":\"Januarius Njoku is a mechanical engineer who is passionate about using technology to improve the design and performance of mechanical systems. With experience in programming with C# and Python, he is also well-versed in DevOps technologies such as Ansible, Docker, and Puppet. Moreover, having a knack for writing, he has authored several technical articles on C# and .NET technologies. He is always on the lookout for new challenges and opportunities to learn and grow in the field of engineering and technology.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/januariusnjoku\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/cjaynjoku\"],\"url\":\"https:\/\/code-maze.com\/author\/njokujennaro\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Read and Parse a JSON File in C#","description":"In this article, we are going to discuss six different methods that we can use to read and parse a JSON file in C#.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/","og_locale":"en_US","og_type":"article","og_title":"How to Read and Parse a JSON File in C#","og_description":"In this article, we are going to discuss six different methods that we can use to read and parse a JSON file in C#.","og_url":"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/","og_site_name":"Code Maze","article_published_time":"2023-03-03T06:00:34+00:00","article_modified_time":"2024-04-04T15:50:06+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Januarius Njoku","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/cjaynjoku","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Januarius Njoku","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/"},"author":{"name":"Januarius Njoku","@id":"https:\/\/code-maze.com\/#\/schema\/person\/c9d04eed48c29b9b10e3a0ec0fa77a18"},"headline":"How to Read and Parse a JSON File in C#","datePublished":"2023-03-03T06:00:34+00:00","dateModified":"2024-04-04T15:50:06+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/"},"wordCount":1549,"commentCount":2,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["JSON","Json.NET","Newtonsoft.Json","System.Text.Json"],"articleSection":["C#","JSON"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/","url":"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/","name":"How to Read and Parse a JSON File in C#","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2023-03-03T06:00:34+00:00","dateModified":"2024-04-04T15:50:06+00:00","description":"In this article, we are going to discuss six different methods that we can use to read and parse a JSON file in C#.","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-read-and-process-json-file\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-read-and-process-json-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"How to Read and Parse a JSON File in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/c9d04eed48c29b9b10e3a0ec0fa77a18","name":"Januarius Njoku","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/01\/Januarius-Njoku-150x150.jpeg","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/01\/Januarius-Njoku-150x150.jpeg","caption":"Januarius Njoku"},"description":"Januarius Njoku is a mechanical engineer who is passionate about using technology to improve the design and performance of mechanical systems. With experience in programming with C# and Python, he is also well-versed in DevOps technologies such as Ansible, Docker, and Puppet. Moreover, having a knack for writing, he has authored several technical articles on C# and .NET technologies. He is always on the lookout for new challenges and opportunities to learn and grow in the field of engineering and technology.","sameAs":["https:\/\/www.linkedin.com\/in\/januariusnjoku\/","https:\/\/x.com\/https:\/\/twitter.com\/cjaynjoku"],"url":"https:\/\/code-maze.com\/author\/njokujennaro\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/83203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=83203"}],"version-history":[{"count":7,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/83203\/revisions"}],"predecessor-version":[{"id":116274,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/83203\/revisions\/116274"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=83203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=83203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=83203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}