JSON to Code

JSON to C#

Paste a JSON sample. Get C# types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.

Open the tool with C# chosen

An example

This JSON:

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "joined": "2020-01-02T03:04:05Z",
  "tags": ["engineer", "writer"],
  "address": { "city": "London", "postcode": null },
  "scores": [9.5, 8, 10]
}

becomes this C#:

namespace QuickType
{

    public partial class Root
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public bool Active { get; set; }
        public DateTimeOffset Joined { get; set; }
        public string[] Tags { get; set; }
        public Address Address { get; set; }
        public double[] Scores { get; set; }
    }

    public partial class Address
    {
        public string City { get; set; }
        public object Postcode { get; set; }
    }
}

With the reader and the writer

The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.

// <auto-generated />
//
// To parse this JSON data, add NuGet 'System.Text.Json' then do:
//
//    using QuickType;
//
//    var root = Root.FromJson(jsonString);
#nullable enable
#pragma warning disable CS8618
#pragma warning disable CS8601
#pragma warning disable CS8602
#pragma warning disable CS8603

namespace QuickType
{
    using System;
    using System.Collections.Generic;

    using System.Text.Json;
    using System.Text.Json.Serialization;
    using System.Globalization;

    public partial class Root
    {
        [JsonPropertyName("id")]
        public long Id { get; set; }

        [JsonPropertyName("name")]
        public string Name { get; set; }

        [JsonPropertyName("active")]
        public bool Active { get; set; }

        [JsonPropertyName("joined")]
        public DateTimeOffset Joined { get; set; }

        [JsonPropertyName("tags")]
        public string[] Tags { get; set; }

        [JsonPropertyName("address")]
        public Address Address { get; set; }

        [JsonPropertyName("scores")]
        public double[] Scores { get; set; }
    }

and 148 more lines. Open the tool to see the whole file.

What you need in your project

`System.Text.Json`, or Newtonsoft, depending on the option.

The comment at the top of the file names the package to add. Both readers are in common use; the newer one is in the framework.

The code marks the file as generated, so a code review tool leaves it alone.

Turn on "Types only" when your project already holds the reading code.

The trap to know about

A nullable reference needs the feature turned on in the project. The file turns it on at the top; remove that line if your project sets it elsewhere.

The options you can change

These belong to C#, so the row of switches changes with the language.

OptionWhat it doesDefault
FrameworkSerialization frameworkNewtonSoft | SystemTextJson
Array typeUse T[] or List<T>array | list
NamespaceGenerated namespaceQuickType
Generate virtual propertiesGenerate virtual propertiesfalse
FeaturesOutput featurescomplete | attributes-only
Types onlyPlain types onlyfalse
Fail if required properties are missingFail if required properties are missingfalse
Keep original field name generateKeep original field name generatefalse

Questions

How do I turn JSON into C#?

Paste your JSON into the box, choose C#, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.

Does my JSON go to a server?

No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.

What do I need to add to my C# project?

`System.Text.Json`, or Newtonsoft, depending on the option.

How does it know which fields are optional?

From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.

Other languages

JSON to TypeScript JSON to Python JSON to Go JSON to Java JSON to Rust All the guides

Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.

Open JSON to Code