You can use the C# dynamic type to make things easier. This technique also makes re-factoring simpler as it does not rely on magic-strings.

JSON

The JSON string below is a simple response from an HTTP API call, and it defines two properties: Id and Name.

{"Id": 1, "Name": "biofractal"}

C#

Use JsonConvert.DeserializeObject<dynamic>() to deserialize this string into a dynamic type then simply access its properties in the usual way.

dynamic results = JsonConvert.DeserializeObject<dynamic>(json);
var id = results.Id;
var name= results.Name;

If you specify the type of the results variable as dynamic, instead of using the var keyword, then the property values will correctly deserialize, e.g. Id to an int and not a JValue (thanks to GFoley83 for the comment below).

Note: The NuGet link for the Newtonsoft assembly is http://nuget.org/packages/newtonsoft.json.

Package: You can also add the package with nuget live installer, with your project opened just do browse package and then just install it install, unistall, update, it will just be added to your project under Dependencies/NuGet

Answer from biofractal on Stack Overflow
🌐
Medium
medium.com › @ieplt › comprehensive-guide-to-using-json-and-newtonsoft-json-in-net-ac67b7963e35
Comprehensive Guide to Using JSON and Newtonsoft.Json in .NET | by İbrahim Emre POLAT | Medium
June 14, 2024 - Newtonsoft.Json makes it simple to deserialize JSON strings into .NET objects. Let’s explore how to deserialize JSON into objects using Newtonsoft.Json. To deserialize JSON into a C# object, you can use the JsonConvert.DeserializeObject<T> method. Here's a basic example:
🌐
Newtonsoft
newtonsoft.com › json › help › html › Samples.htm
Samples
Over 100 code samples covering Json.NET's most commonly used functionality.
🌐
Newtonsoft
newtonsoft.com › json › help › html › serializingjson.htm
Serializing and Deserializing JSON
The quickest method of converting between JSON text and a .NET object is using the T:Newtonsoft.Json.JsonSerializer. The JsonSerializer converts .NET objects into their JSON equivalent and back again by mapping the .
🌐
Newtonsoft
newtonsoft.com › json › help › html › readingwritingjson.htm
Basic Reading and Writing JSON
They are located in the Newtonsoft.Json.Linq namespace. These objects allow you to use LINQ to JSON objects with objects that read and write JSON, such as the JsonSerializer. For example you can deserialize from a LINQ to JSON object into a regular .NET object and vice versa.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › standard › serialization › system-text-json › migrate-from-newtonsoft
Migrate from Newtonsoft.Json to System.Text.Json - .NET | Microsoft Learn
Newtonsoft.Json accepts non-string values, such as a number or the literals true and false, for deserialization to properties of type string. Here's an example of JSON that Newtonsoft.Json successfully deserializes to the following class:
🌐
GitHub
github.com › JamesNK › Newtonsoft.Json
GitHub - JamesNK/Newtonsoft.Json: Json.NET is a popular high-performance JSON framework for .NET · GitHub
Json.NET is a popular high-performance JSON framework for .NET - JamesNK/Newtonsoft.Json
Starred by 11.3K users
Forked by 3.3K users
Languages   C#
Top answer
1 of 12
296

You can use the C# dynamic type to make things easier. This technique also makes re-factoring simpler as it does not rely on magic-strings.

JSON

The JSON string below is a simple response from an HTTP API call, and it defines two properties: Id and Name.

{"Id": 1, "Name": "biofractal"}

C#

Use JsonConvert.DeserializeObject<dynamic>() to deserialize this string into a dynamic type then simply access its properties in the usual way.

dynamic results = JsonConvert.DeserializeObject<dynamic>(json);
var id = results.Id;
var name= results.Name;

If you specify the type of the results variable as dynamic, instead of using the var keyword, then the property values will correctly deserialize, e.g. Id to an int and not a JValue (thanks to GFoley83 for the comment below).

Note: The NuGet link for the Newtonsoft assembly is http://nuget.org/packages/newtonsoft.json.

Package: You can also add the package with nuget live installer, with your project opened just do browse package and then just install it install, unistall, update, it will just be added to your project under Dependencies/NuGet

2 of 12
278

If you just need to get a few items from the JSON object, I would use Json.NET's LINQ to JSON JObject class. For example:

JToken token = JObject.Parse(stringFullOfJson);

int page = (int)token.SelectToken("page");
int totalPages = (int)token.SelectToken("total_pages");

I like this approach because you don't need to fully deserialize the JSON object. This comes in handy with APIs that can sometimes surprise you with missing object properties, like Twitter.

Documentation: Serializing and Deserializing JSON with Json.NET and LINQ to JSON with Json.NET

🌐
Newtonsoft
newtonsoft.com › json › help › html › introduction.htm
Introduction
It is used in many major open source projects, including: Mono, an open source implementation of the .NET framework; RavenDB, a JSON based document database; ASP.NET SignalR, an async library for building real-time, multi-user interactive web applications; and ASP.NET Core, Microsoft's web ...
Find elsewhere
🌐
Newtonsoft
newtonsoft.com › json › help › html › CreatingLINQtoJSON.htm
Creating JSON
List<Post> posts = GetPosts(); JObject rss = new JObject( new JProperty("channel", new JObject( new JProperty("title", "James Newton-King"), new JProperty("link", "http://james.newtonking.com"), new JProperty("description", "James Newton-King's blog."), new JProperty("item", new JArray( from ...
🌐
NuGet
nuget.org › packages › newtonsoft.json
NuGet Gallery | Newtonsoft.Json 13.0.4
JArray array = new JArray(); array.Add("Manual text"); array.Add(new DateTime(2000, 5, 23)); JObject o = new JObject(); o["MyArray"] = array; string json = o.ToString(); // { // "MyArray": [ // "Manual text", // "2000-05-23T00:00:00" // ] // ...
🌐
ZetCode
zetcode.com › csharp › json-net
C# Json.NET - working with JSON with Newtonsoft Json.NET
In this article we work with Newtonsoft Json.NET library. In the standard library, we can alternatively use System.Text.Json. JsonConvert provides methods for converting between .NET types and JSON types. JsonConvert.SerializeObject serializes the specified object to a JSON string. JsonConvert.DeserializeObject deserializes the JSON to a .NET object. In the following example, we serialize an object to a JSON string.
🌐
FriendlyUsers Tech Blog
friendlyuser.github.io › posts › tech › 2023 › A_Comprehensive_Guide_to_Newtonsoft.Json
A Comprehensive Guide to Newtonsoft.Json - FriendlyUsers Tech Blog
Similar to serialization, JsonConvert provides various options to customize the deserialization process. You can use attributes, custom converters, and specify error handling during deserialization. Here’s an example that demonstrates some of these features: using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System; public class Person { [JsonProperty("full_name")] public string Name { get; set; } public int Age { get; set; } [JsonConverter(typeof(StringEnumConverter))] public Gender Gender { get; set; } } public enum Gender { Male, Female } public class Program { public static v
🌐
C# Corner
c-sharpcorner.com › article › newtonsoft-json-deserialize-c-sharp-example
Newtonsoft JSON Deserialize in C# with Example
August 17, 2023 - Step 6. Create a new class, “module post,” for requesting json parse from the rest api client. Write the following program listing. using System; using System.Collections.Generic; using System.Linq; using System.Text; using Newtonsoft.Json.Linq; using System.Net; using System.IO; namespace newtonsoft_json { public class modulePost { public static string urlServer = "http://localhost/json/ws.php"; public void setURL(string url) { urlServer = url; } public string getURL() { return urlServer; } public string Post(string data) { string DataFromPHP = null; try { string url = getURL(); byte[] bu
🌐
Newtonsoft
newtonsoft.com › json
Json.NET - Newtonsoft
Create, parse, query and modify JSON using Json.NET's JObject, JArray and JValue objects.
🌐
Newtonsoft
newtonsoft.com › json › help › html › ParseJsonObject.htm
Parsing JSON Object using JObject.Parse
string json = @"{ CPU: 'Intel', Drives: [ 'DVD read/writer', '500 gigabyte hard drive' ] }"; JObject o = JObject.Parse(json); Console.WriteLine(o.ToString()); // { // "CPU": "Intel", // "Drives": [ // "DVD read/writer", // "500 gigabyte hard drive" // ] // }
🌐
Newtonsoft
newtonsoft.com › json › help › html › DeserializeWithJsonSerializerFromFile.htm
Deserialize JSON from a file
// read file into a string and deserialize JSON to a type Movie movie1 = JsonConvert.DeserializeObject<Movie>(File.ReadAllText(@"c:\movie.json")); // deserialize JSON directly from a file using (StreamReader file = File.OpenText(@"c:\movie.json")) { JsonSerializer serializer = new JsonSerializer(); ...
🌐
Newtonsoft
newtonsoft.com › json › help › html › DeserializeObject.htm
Deserialize an Object
string json = @"{ 'Email': 'james@example.com', 'Active': true, 'CreatedDate': '2013-01-20T00:00:00Z', 'Roles': [ 'User', 'Admin' ] }"; Account account = JsonConvert.DeserializeObject<Account>(json); Console.WriteLine(account.Email); // james@example.com
🌐
Particular
docs.particular.net › nservicebus › serialization › newtonsoft
Json.NET Serializer • Newtonsoft Serializer • Particular Docs
3 weeks ago - var serialization = endpointConfiguration.UseSerialization<NewtonsoftJsonSerializer>(); serialization.ReaderCreator(stream => { var streamReader = new StreamReader(stream, Encoding.UTF8); return new JsonTextReader(streamReader); }); Customize the creation of the JsonWriter. In the example below, the custom writer omits the Byte Order Mark (BOM).
🌐
Newtonsoft
newtonsoft.com › json › help › html › ReadJson.htm
Read JSON from a file
JObject o1 = JObject.Parse(File.ReadAllText(@"c:\videogames.json")); // read JSON directly from a file using (StreamReader file = File.OpenText(@"c:\videogames.json")) using (JsonTextReader reader = new JsonTextReader(file)) { JObject o2 = (JObject)JToken.ReadFrom(reader); }
🌐
Particular
docs.particular.net › samples › serializers › newtonsoft
Newtonsoft JSON Serializer sample • Newtonsoft Serializer Samples • Particular Docs
February 18, 2026 - This sample uses the Newtonsoft serializer NServiceBus.Newtonsoft.Json to provide full access to the Newtonsoft Json.net API.