Flattener 1.1.1
dotnet add package Flattener --version 1.1.1
NuGet\Install-Package Flattener -Version 1.1.1
<PackageReference Include="Flattener" Version="1.1.1" />
<PackageVersion Include="Flattener" Version="1.1.1" />
<PackageReference Include="Flattener" />
paket add Flattener --version 1.1.1
#r "nuget: Flattener, 1.1.1"
#:package Flattener@1.1.1
#addin nuget:?package=Flattener&version=1.1.1
#tool nuget:?package=Flattener&version=1.1.1
Flattener
Description
Flattener converts JSON or XML into flat key-value collections with dot notation.
New in v1.1.1
- Dependency update:
System.Text.Json10.0.12 - No API or behavior changes
New in v1.1.0
TryFlatten(input, out result, out error, includeNullItems)onJsonFlattenerandXmlFlattenerreports malformed or null input instead of silently returning an empty collectionFlatten(input, includeNullItems, throwOnError)overload surfaces the underlyingJsonException/XmlException- Existing
Flatten(input, includeNullItems)behavior is unchanged
Simple Examples
JSON
using System.Collections.Specialized;
using Flattener;
string json = @"{ ""person"": { ""name"": ""John"", ""age"": 30 } }";
NameValueCollection flattened = JsonFlattener.Flatten(json);
// By default, null values are excluded
// To include nulls:
NameValueCollection withNulls = JsonFlattener.Flatten(json, includeNullItems: true);
// Accessing values
// For a key with a single value:
string name = flattened.Get("person.name"); // Returns "John"
// For a key that might have multiple values:
string[] skills = flattened.GetValues("skills"); // Returns array of values or null
foreach (string key in flattened.AllKeys)
{
string[] values = flattened.GetValues(key);
foreach (string value in values)
{
Console.WriteLine($"{key} = {value ?? "null"}");
}
}
XML
using System.Collections.Specialized;
using Flattener;
string xml = @"<User id=""123""><n>Alice</n><Skills><Skill>C#</Skill></Skills></User>";
NameValueCollection flattened = XmlFlattener.Flatten(xml);
// By default, empty elements are excluded
// To include empty elements:
NameValueCollection withEmpties = XmlFlattener.Flatten(xml, includeNullItems: true);
// Accessing values
// XML attributes are prefixed with @
string id = flattened.Get("User.@id"); // Returns "123"
string name = flattened.Get("n"); // Returns "Alice" (child elements are keyed relative to the root)
// For repeated elements (array-like):
string[] skills = flattened.GetValues("Skills.Skill");
foreach (string key in flattened.AllKeys)
{
string[] values = flattened.GetValues(key);
foreach (string value in values)
{
Console.WriteLine($"{key} = {value ?? "null"}");
}
}
Detecting malformed input
Flatten(input) returns an empty collection for malformed input, which looks the same as a valid document with no values. When the difference matters, for example to fail a request or record the error on a trace span, use TryFlatten or the throwOnError overload:
using System;
using System.Collections.Specialized;
using Flattener;
if (JsonFlattener.TryFlatten(json, out NameValueCollection flattened, out Exception error))
{
// flattened holds the key-value pairs (possibly zero for a valid empty document)
}
else
{
// error is a JsonException (malformed input) or ArgumentNullException (null input);
// flattened is an empty collection, never null
Console.WriteLine($"Invalid JSON: {error.Message}");
}
// Or let the parse exception propagate:
NameValueCollection strict = XmlFlattener.Flatten(xml, includeNullItems: false, throwOnError: true);
TryFlatten doesn't throw for null or malformed input. Flatten(..., throwOnError: true) throws System.Text.Json.JsonException (JSON) or System.Xml.XmlException (XML) for malformed input, and ArgumentNullException for null input.
Version history
Refer to CHANGELOG.md.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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 is compatible. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Text.Json (>= 10.0.12)
-
.NETStandard 2.1
- System.Text.Json (>= 10.0.12)
-
net10.0
- No dependencies.
-
net8.0
- System.Text.Json (>= 10.0.12)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Updated System.Text.Json to 10.0.12