JsonFlatten 1.0.4

dotnet add package JsonFlatten --version 1.0.4                
NuGet\Install-Package JsonFlatten -Version 1.0.4                
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.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JsonFlatten --version 1.0.4                
#r "nuget: JsonFlatten, 1.0.4"                
#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.
// Install JsonFlatten as a Cake Addin
#addin nuget:?package=JsonFlatten&version=1.0.4

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

JsonFlatten

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

This library is compatible with both .NET and .NET Framework.

Example

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. 
        var flattened = jObj.Flatten();		
        // or Dictionary<string, object> flattened = new Dictionary<string, object>(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 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 was computed.  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. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.1 is compatible.  netstandard1.2 was computed.  netstandard1.3 was computed.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 was computed.  netstandard2.1 was computed. 
.NET Framework net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  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 tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Windows Phone wpa81 was computed. 
Windows Store netcore was computed.  netcore45 was computed.  netcore451 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on JsonFlatten:

Package Downloads
Fathym

The Fathym reference architecture projects provide the foundation for building Fathym's dotnet applications.

IglooSoftware.Sdk.EventSourcing

Igloo Event Sourcing SDK for services.

LoopNet

Unofficial Loopring SDK in .NET 7

JsonataQueryBuilder

Generate jsonata Query.

M.Util.FlatObjec

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 2,755 11/21/2024
1.0.3 46,249 7/16/2024
1.0.2 1,962,514 3/12/2022
1.0.1 1,926,748 5/11/2019