Blazor.JsonEditor 1.1.1

dotnet add package Blazor.JsonEditor --version 1.1.1
                    
NuGet\Install-Package Blazor.JsonEditor -Version 1.1.1
                    
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="Blazor.JsonEditor" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Blazor.JsonEditor" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="Blazor.JsonEditor" />
                    
Project file
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 Blazor.JsonEditor --version 1.1.1
                    
#r "nuget: Blazor.JsonEditor, 1.1.1"
                    
#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 Blazor.JsonEditor@1.1.1
                    
#: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=Blazor.JsonEditor&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=Blazor.JsonEditor&version=1.1.1
                    
Install as a Cake Tool

Blazor.JsonEditor

Json Editor and Viewer for Blazor Server App and Wasm. Rewrited and working version of Blazoring.JsonEditor

Demo:

Click me to see the demo

Json Editor and Viewer tool

  • To install the package run following command:

Install-Package Blazor.JsonEditor or search Blazor.JsonEditor in Nuget gallery.

This will install Blazor.JsonEditor in your project. You also need to add in _Imports.razor:

@using Blazor.JsonEditor.Component

Also, you need to add javascript file in _Host.cshtml ( Server app) or index.html ( Wasm) file:

<script src="_content/Blazor.JsonEditor/Blazor.JsonEditor.js"></script>

For icons suppor JsonEditor uses Font-Awesome icons library. You need to add link to _Host.cshtml ( Server app) or index.html ( Wasm) file:

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.4.2/css/all.css">

Using in code to have an editor:

<EditForm Model="DemoJson">
    <JsonEditor @bind-Value="DemoJson.Json" FieldName="@nameof(IndexModel.Json)" ValidationFor="@(() => DemoJson.Json)"></JsonEditor>
</EditForm>

Using in code to have a viewer:

<EditForm Model="DemoJson">
    <JsonEditor @bind-Value="DemoJson.Json" FieldName="@nameof(IndexModel.Json)" ValidationFor="@(() => DemoJson.Json)" AllowEdit="false"></JsonEditor>
</EditForm>

Blazor.JsonEditor doesn't work without EditForm. Also, validation is required.

Customization

Editor template:

You can customize and pass your own editor template as dynamic component. Editor template is everything that you see after property name and value like buttons and editor window itself. For this you need to set parameter CustomEditor. As an example CustomEditor="typeof(JsonItemCustomEditor)"

<EditForm Model="DemoJson">
    <JsonEditor @bind-Value="DemoJson.Json" FieldName="@nameof(IndexModel.Json)" ValidationFor="@(() => DemoJson.Json)" CustomEditor="typeof(JsonItemCustomEditor)"></JsonEditor>
</EditForm>

In your custom component you need to have parameters:

//Main Json object
[Parameter] 
public JsonObject? JsonObject { get; set; }

//Event that you need to invoke on every object change
[Parameter] 
public EventCallback<JsonObject> JsonObjectChanged { get; set; }

//Values for dropdown
[Parameter] 
public Dictionary<string, string>? KeyValues { get; set; }

//Edit item in case of edit
[Parameter]
public KeyValuePair<string, JsonNode?>? JsonItemToEdit { get; set; }

Also on add and edit you need to pass data to JsonHelper. We have two methods for this: AddNodeValue and EditNodeValue

public static void AddNodeValue(JsonObject jsonObject, JsonItem jsonItem)

public static void EditNodeValue(JsonObject jsonObject, JsonItem jsonItem, string editPropertyName)

After Add/Edit/Delete do not forget to Invoke JsonObjectChanged

await JsonObjectChanged.InvokeAsync(JsonObject);

View templates:

You can customize view template for an item and an object. Item is key: value template. Object view template is an object template that contains a lot of items template in it.

{
  "Nullable" : null, //item view template
  "String" : "random", //item view template
  "Number" : 1, //item view template
  "Array" : [1,2,3], //item view template
  "True" : true, //item view template
  "False" : false, //item view template
  "Object" : { //!OBJECT! view template
    "Nullable" : null, //item view template
    "String" : "random", //item view template
    "Number" : 1, //item view template
    "Array" : [1,2,3], //item view template
    "True" : true, //item view template
    "False" : false //item view template
  }
}

Item view template:

<EditForm Model="DemoJson">
    <JsonEditor @bind-Value="DemoJson.Json" FieldName="@nameof(IndexModel.Json)" ValidationFor="@(() => DemoJson.Json)" CustomItemView="typeof(JsonItemCustomView)"></JsonEditor>
</EditForm>

In your custom component you need to have parameters:

[Parameter]
public KeyValuePair<string, JsonNode?> JsonItem { get; set; }

You can find example in repository.

Object view template:

<EditForm Model="DemoJson">
    <JsonEditor @bind-Value="DemoJson.Json" FieldName="@nameof(IndexModel.Json)" ValidationFor="@(() => DemoJson.Json)" CustomObjectView="typeof(JsonObjectCustomView)"></JsonEditor>
</EditForm>

In your custom component you need to have parameters:

[Parameter]
        public KeyValuePair<string, JsonNode?> JsonItem { get; set; }
        
        [Parameter]
        public EventCallback<string?> ValueChanged { get; set; }

        [Parameter]
        public Dictionary<string, string>? KeyValues { get; set; }
        
        [Parameter] 
        public bool AllowEdit { get; set; } = true;
        
        [Parameter] 
        public Type? CustomEditor { get; set; }
        
        [Parameter] 
        public Type? CustomItemView { get; set; }
        
        [Parameter] 
        public Type? CustomObjectView { get; set; }

Don't forget to include InternalJsonEditor inside you custom template to allow build lower object level.

Example:

<InternalJsonEditor Value="@JsonItem.Value.ToJsonString()"
                    ValueChanged="ValueChanged"
                    KeyValues="KeyValues"
                    AllowEdit="AllowEdit"
                    CustomEditor="CustomEditor"
                    CustomItemView="CustomItemView"
                    CustomObjectView="CustomObjectView">
</InternalJsonEditor>

You can find example in repository.

Next steps

  • Refactor

If you like it please support:

Buy me a coffee

Ko-Fi

GitHub

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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.  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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.1 28,539 2/24/2025
1.1.0 15,182 5/27/2024
1.0.7 544 5/9/2024
1.0.6 4,311 10/16/2023
1.0.5 238 10/15/2023
1.0.4 266 10/14/2023

Item view and object view customization added