JsonField

View as Markdown

JsonField

JsonField describes a field with data_type set to json, such as a JSON Rich Text Editor (JSON RTE). It derives from TextboxField and adds the content types that the editor can embed entries from.

When to Use This Class: Use this class to add a JSON RTE field to a content type, or to read one from a fetched schema.

Conceptual Role: Acts as the Field subclass that the SDK creates for a field with data_type set to json.

Returns: JsonField. A JSON field of a schema.

Properties

JsonField inherits every property of TextboxField and Field, and adds the property below.

NameTypeRequiredDefaultDescription
ReferenceToJsonElement?NonullUIDs of the content types whose entries the editor can embed. Serialized as reference_to.

Class-Level Notes

JSON RTE settings.

  • Set FieldMetadata.AllowJsonRte to true and FieldMetadata.RichTextType to an editor type such as advanced. The Create content type with JSON RTE request of the Content Management API reference shows these settings. Set FieldMetadata.EmbedEntry to true to allow embedded entries.
  • ReferenceTo is a JsonElement. Create it with JsonSerializer.SerializeToElement(new[] { "author" }).

Method Index

JsonField has no methods. Set its properties and add it to ContentModelling.Schema.

Class-Level Snippet

The snippet below creates a content type with a JSON RTE field that can embed author entries.

using System;
using System.Collections.Generic;
using System.Text.Json;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Fields;

ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");

ContentModelling model = new ContentModelling
{
    Title = "Blog Post",
    Uid = "blog_post",
    Schema = new List<Field>
    {
        new TextboxField { DisplayName = "Title", Uid = "title", DataType = "text", Mandatory = true, Unique = true },
        new JsonField
        {
            DisplayName = "Body",
            Uid = "body",
            DataType = "json",
            ReferenceTo = JsonSerializer.SerializeToElement(new[] { "author" }),
            FieldMetadata = new FieldMetadata { AllowJsonRte = true, RichTextType = "advanced", EmbedEntry = true }
        }
    }
};

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").ContentType().Create(model);
    Console.WriteLine(response.OpenJsonObjectResponse()["notice"]);
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}