Philiprehberger.FlattenJson
0.1.10
dotnet add package Philiprehberger.FlattenJson --version 0.1.10
NuGet\Install-Package Philiprehberger.FlattenJson -Version 0.1.10
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Philiprehberger.FlattenJson" Version="0.1.10" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Philiprehberger.FlattenJson" Version="0.1.10" />
<PackageReference Include="Philiprehberger.FlattenJson" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Philiprehberger.FlattenJson --version 0.1.10
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Philiprehberger.FlattenJson, 0.1.10"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Philiprehberger.FlattenJson@0.1.10
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Philiprehberger.FlattenJson&version=0.1.10
#tool nuget:?package=Philiprehberger.FlattenJson&version=0.1.10
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Philiprehberger.FlattenJson
Flatten nested JSON objects into dot-notation key-value pairs, and unflatten them back.
Installation
dotnet add package Philiprehberger.FlattenJson
Usage
using Philiprehberger.FlattenJson;
// Flatten nested JSON
var flat = JsonFlattener.Flatten("""{"a":{"b":1,"c":[2,3]}}""");
// flat["a.b"] == "1"
// flat["a.c.0"] == "2"
// flat["a.c.1"] == "3"
// Null values are preserved
var flat2 = JsonFlattener.Flatten("""{"x":null,"y":{"z":42}}""");
// flat2["x"] == null
// flat2["y.z"] == "42"
// Custom separator
var flat3 = JsonFlattener.Flatten("""{"a":{"b":1}}""", separator: "/");
// flat3["a/b"] == "1"
// Unflatten back to JSON
var json = JsonFlattener.Unflatten(flat);
// => {"a":{"b":"1","c":["2","3"]}}
Arrays
using Philiprehberger.FlattenJson;
var json = """{"users": [{"name": "Alice"}, {"name": "Bob"}]}""";
var flat = JsonFlattener.Flatten(json);
// { "users.0.name": "Alice", "users.1.name": "Bob" }
Custom Separator
using Philiprehberger.FlattenJson;
var json = """{"db": {"host": "localhost", "port": "5432"}}""";
var flat = JsonFlattener.Flatten(json, "/");
// { "db/host": "localhost", "db/port": "5432" }
Unflatten
using Philiprehberger.FlattenJson;
var flat = new Dictionary<string, string?>
{
["db.host"] = "localhost",
["db.port"] = "5432"
};
var json = JsonFlattener.Unflatten(flat);
// {"db":{"host":"localhost","port":"5432"}}
API
JsonFlattener
| Method | Description |
|---|---|
Flatten(string json, string separator = ".") |
Parse json and return a flat Dictionary<string, string?> using dot-notation keys |
Unflatten(Dictionary<string, string?> flat, string separator = ".") |
Reconstruct a JSON string from a flat dictionary |
Notes:
- All leaf values are stored as strings (or
nullfor JSON nulls). - Array elements are keyed by their zero-based index:
"a.0","a.1", etc. - Uses
System.Text.Json— no external dependencies.
Development
dotnet build src/Philiprehberger.FlattenJson.csproj --configuration Release
Support
If you find this project useful:
License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.