HOWTO · Csharp

How to Convert a JSON String to a JObject in C#

Parse JSON text into a JObject in C# with Newtonsoft.Json, read nested values, handle parsing errors, and serialize changes.

On this page

To convert JSON text into an in-memory object in C#, install Newtonsoft.Json and call JObject.Parse() with the text. The method returns a JObject whose properties you can read or update. The input must already be valid JSON; parsing does not turn arbitrary plain text into JSON.

Convert a JSON String to a JObject with JObject.Parse()

Install the package with dotnet add package Newtonsoft.Json. The conversion itself is JObject person = JObject.Parse(jsonText);; JObject.Parse() is part of Newtonsoft.Json and requires that package to be available to the project. The following self-contained C# example uses the built-in System.Text.Json API to show the same input and property-reading flow without that external dependency. It creates a JsonDocument, not a JObject.

using System;
using System.Text.Json;

class Program
{
    static void Main()
    {
        string jsonText = "{\"name\":\"John\",\"address\":{\"city\":\"New York\"}}";
        using (JsonDocument person = JsonDocument.Parse(jsonText))
        {
            Console.WriteLine(person.RootElement.GetProperty("name").GetString());
            Console.WriteLine(person.RootElement.GetProperty("address").GetProperty("city").GetString());
        }
    }
}

Here, jsonText is a string and person is an in-memory JSON document. For a Newtonsoft.Json JObject, use JObject.Parse(jsonText) as shown above; its indexer returns a JSON token, which can represent a string, number, nested object, array, or another JSON value. The built-in example demonstrates the same JSON shape with System.Text.Json and does not create a JObject.

Use JObject when the root of the JSON text is an object with named properties. If the root is an array, use Newtonsoft.Json’s JArray API instead. Keep the original text in a string variable, then pass that variable to the parser; do not add another set of quotes around the whole JSON document.

Handle Invalid JSON

JObject.Parse() expects a JSON object. Malformed JSON or a top-level value other than an object can raise JsonReaderException. When input comes from an external source, catch that specific exception and decide how the application should report or log the problem. The C# pattern is try { JObject person = JObject.Parse(jsonText); } catch (JsonReaderException) { Console.WriteLine("The JSON text could not be parsed."); }. This is an unexecuted syntax illustration because the supplied runtime lacks Newtonsoft.Json. Keep the catch focused on parsing errors; log parser details in application code instead of exposing them to end users.

The catch handles failures from parsing, so the rest of the application can choose a useful fallback, ask for corrected input, or record a diagnostic. If the input is already trusted and malformed data cannot occur, the caller may instead let the exception propagate to its normal error handler. Parsing only checks JSON structure; it does not verify that required application fields are present or have the expected business meaning.

Read Values from a JObject

Use an indexer such as person["name"] to read a top-level property. Chain indexers for nested values, as in person["address"]?["city"]; the null-conditional operator stops the lookup if the intermediate property is absent or null. Typed accessors convert tokens to .NET values: for example, person.Value<string>("name") or person["age"]?.Value<int>(). Choose a type that matches the JSON value and handle a missing property when your application requires it.

The indexer is convenient when you want to keep working with JSON tokens, while Value<T>() is useful when a .NET string, integer, or other typed value is needed. A null result from a safe lookup lets your code distinguish a missing intermediate value before using it. For required data, check the token and report which property is missing rather than assuming every input has the same shape.

Modify and Serialize a JObject

Assign to an existing property to update it, or use a new property name to add a value. Call ToString() to produce formatted JSON text. For example, the C# operations are person["age"] = 31;, person["city"] = "New York";, and string updatedJson = person.ToString();. These API calls are unexecuted illustrations because the supplied runtime lacks Newtonsoft.Json. The resulting string represents the updated in-memory object as JSON and can be passed to an API or storage layer that expects JSON.

Mutation changes the in-memory JObject; it does not change the original JSON string variable. Serialize after all edits are complete, and use the resulting text wherever the next step expects JSON. If compact output is preferable, Newtonsoft.Json also supports formatting options for serialization; choose formatting based on whether people or another program will read the text.