JsonFlatten 1.0.2

.NET Standard 1.1
dotnet add package JsonFlatten --version 1.0.2
NuGet\Install-Package JsonFlatten -Version 1.0.2
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="JsonFlatten" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JsonFlatten --version 1.0.2
#r "nuget: JsonFlatten, 1.0.2"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install JsonFlatten as a Cake Addin
#addin nuget:?package=JsonFlatten&version=1.0.2

// Install JsonFlatten as a Cake Tool
#tool nuget:?package=JsonFlatten&version=1.0.2

JsonFlatten

Extension methods to flatten a JSON.NET JObject to an IDictionary<string, object> or vice versa.

Usage

https://dotnetfiddle.net/jYghhm

This:

{
  "$id": "https://example.com/arrays.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "description": "A representation of a person, company, organization, or place",
  "type": "object",
  "properties": {
    "fruits": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "vegetables": {
      "type": "array",
      "items": { "$ref": "#/definitions/veggie" }
    }
  },
  "definitions": {
    "veggie": {
      "type": "object",
      "required": [ "veggieName", "veggieLike" ],
      "properties": {
        "veggieName": {
          "type": "string",
          "description": "The name of the vegetable."
        },
        "veggieLike": {
          "type": "boolean",
          "description": "Do I like this vegetable?"
        }
      }
    }
  }
}

Becomes this:

{
    "$id": "https://example.com/arrays.schema.json",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "description": "A representation of a person, company, organization, or place",
    "type": "object",
    "properties.fruits.type": "array",
    "properties.fruits.items.type": "string",
    "properties.vegetables.type": "array",
    "properties.vegetables.items.$ref": "#/definitions/veggie",
    "definitions.veggie.type": "object",
    "definitions.veggie.required[0]": "veggieName",
    "definitions.veggie.required[1]": "veggieLike",
    "definitions.veggie.properties.veggieName.type": "string",
    "definitions.veggie.properties.veggieName.description": "The name of the vegetable.",
    "definitions.veggie.properties.veggieLike.type": "boolean",
    "definitions.veggie.properties.veggieLike.description": "Do I like this vegetable?"
}
using System;
using System.Collections.Generic;
using JsonFlatten;
using Newtonsoft.Json.Linq;

public class Program
{
	public static void Main()
	{
		var json = @"{ 'array': [ 1, 2, 3 ], 'boolean': true, 'null': null, 'number': 123, 'object': { 'a': 'b', 'c': 'd', 'e': 'f' }, 'date': '2014-01-01T23:28:56.782Z', 'string': 'Hello World', 'emptyString': '', 'emptyObject': {}, 'emptyArray': [] }";
		JObject jObj = JObject.Parse(json);
		
		// Flatten a JObject to a dictionary. 
		Dictionary<string, object> flattened = new Dictionary<string, object>(jObj.Flatten());
		// or var flattened = jObj.Flatten();
		
		// Retrieve and cast an item from the dictionary
		var date = flattened.Get<DateTime>("date"); // 1/1/2014 11:28:56 PM
        
      		// Unflatten a dictionary to a JObject
		JObject unflattened = flattened.Unflatten();
       		JToken.DeepEquals(jObj, unflattened); // True
		
		// Update an entry
		flattened.Set("date", date.AddDays(5));
        
        	// Try get logic for properties
       		flattened.TryGet<DateTime>("date", out var updatedDate); // updatedDate: 1/6/2014 11:28:56 PM
		
		// Flatten a JObject and exclude any properties that are null or empty e.g. have the value null, "", {}, or []
		var flattenedWithoutEmpty = jObj.Flatten(includeNullAndEmptyValues: false);
	}
}
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard1.1 netstandard1.2 netstandard1.3 netstandard1.4 netstandard1.5 netstandard1.6 netstandard2.0 netstandard2.1
.NET Framework net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen30 tizen40 tizen60
Universal Windows Platform uap uap10.0
Windows Phone wpa81
Windows Store netcore netcore45 netcore451
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on JsonFlatten:

Package Downloads
M.Util.FlatObjec

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 130,002 3/12/2022
1.0.1 1,383,772 5/11/2019