error CS0246: The type or namespace name 'JsonSerializerOptions' could not be found (are you missing a using directive or an assembly reference?) means the compiler cannot see that type from this file. Either the file is missing the using for the type's namespace, or the project is missing the package that contains it. Add the using first; if the using line itself then fails with CS0246, add the package.

For the System.Text.Json types the fix is one line at the top of the file: using System.Text.Json;. For a type that ships in a NuGet package, such as NpgsqlConnection, run dotnet add package Npgsql in the project folder and keep the using.

The error

...\FixLab\Program.cs(7,19): error CS0246: The type or namespace name 'JsonSerializerOptions' could not be found (are you missing a using directive or an assembly reference?) [...\FixLab\FixLab.csproj]
...\FixLab\Program.cs(8,15): error CS0103: The name 'JsonSerializer' does not exist in the current context [...\FixLab\FixLab.csproj]

And the package case, from a second file in the same project:

...\FixLab\Db.cs(1,7): error CS0246: The type or namespace name 'Npgsql' could not be found (are you missing a using directive or an assembly reference?) [...\FixLab\FixLab.csproj]
...\FixLab\Db.cs(5,19): error CS0246: The type or namespace name 'NpgsqlConnection' could not be found (are you missing a using directive or an assembly reference?) [...\FixLab\FixLab.csproj]

The paths are shortened; nothing else was changed.

Why it happens

C# finds a short type name by searching the namespaces imported into the file. With <ImplicitUsings>enable</ImplicitUsings>, which the new-project templates set, the SDK writes a file of global usings under obj. For this console project on SDK 10.0.402 it held exactly these:

// <auto-generated/>
global using System;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Net.Http;
global using System.Threading;
global using System.Threading.Tasks;

That is why List<T> works with no using and JsonSerializer does not: System.Collections.Generic is on the list, System.Text.Json is not. A type used in a declaration, such as new JsonSerializerOptions, gives CS0246; a static call such as JsonSerializer.Serialize gives CS0103 for the same missing using, so the two often arrive together. To see your own project's list, open the *.GlobalUsings.g.cs file under obj.

The second cause is a namespace that no referenced assembly contains. Then the using Npgsql; line itself fails, at column 7, because there is nothing to import. That is the quick way to tell the two apart: an error on a type in the code means a missing using; an error on the using line means a missing package or project reference.

The fix

using System.Text.Json;

var patients = new List<Patient>
{
    new("Maria Garcia", 34),
    new("David Chen", 51)
};

var options = new JsonSerializerOptions { WriteIndented = true };
string json = JsonSerializer.Serialize(patients, options);
Console.WriteLine(json);

record Patient(string Name, int Age);

For the package case:

dotnet add package Npgsql

The first change made the build succeed, and the program printed both patients as indented JSON. The second added <PackageReference Include="Npgsql" Version="10.0.3" /> to the project file, and the build succeeded. If many files need the same namespace, a project-wide using works too: <Using Include="System.Text.Json" /> in an ItemGroup added global using System.Text.Json; to the generated file, and the original Program.cs, without its own using, built and ran.

How it was reproduced

On Windows 11 with .NET SDK 10.0.402: a console project from dotnet new console -n FixLab (net10.0, implicit usings on). Program.cs serialised a list of two fictional patients without using System.Text.Json;, and a second file, Db.cs, created an NpgsqlConnection before the project referenced Npgsql. dotnet add package Npgsql resolved version 10.0.3. Every error and every fix above came from a plain dotnet build.

Frequently asked

Why does List<T> work without a using but JsonSerializer does not?
Implicit usings import a fixed list of namespaces. For a console project on SDK 10 that list includes System.Collections.Generic but not System.Text.Json, so JsonSerializer needs using System.Text.Json; in the file or a project-wide Using item.
How do I know if CS0246 means a missing NuGet package?
Look at where the error points. If the using line itself, such as using Npgsql;, reports CS0246, no referenced assembly has that namespace, so add the package with dotnet add package. If only a type in the code is reported, add the using.
What is the difference between CS0246 and CS0103?
CS0246 is reported when an unknown name is used as a type, for example new JsonSerializerOptions. CS0103 is reported when an unknown name is used as a value or for a static call, for example JsonSerializer.Serialize. The same missing using causes both.

The opposite problem, two imported namespaces that both contain the same name, is CS0104; see CS0104: 'BarcodeReader' is ambiguous. More decoded errors are in the Fixes category.