Implementing JSON Schema-based Intelligent Suggestions in Monaco Editor

Implementing JSON Schema-based code suggestions in Monaco Editor for better JSON editing.

Mar 19, 2025 · 2 min read · 215 Words · -Views -Comments · Digital Life
Monaco Editor Json-Schema Json 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

Recently, while implementing a JSON data editor using Monaco editor, I discovered a method to implement JSON Schema-based intelligent suggestions to enhance user experience. Here’s what I learned.

Implementation

Using Monaco-editor’s language service to implement JSON Schema-based intelligent suggestions.

Setting up JSON Schema

monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
          validate: true,
          schemas: [
 {
              uri: "http://myschema/abi-schema.json",
              fileMatch: [model.uri.toString()],
              schema: {
                type: "array",
                items: {
                  type: "object",
                  required: ["type"],
                  properties: {
                    name: { 
                      type: "string",
                      description: "Function or event name"
 },
                    type: { 
                      type: "string", 
                      enum: ["function", "event", "constructor"],
                      description: "Type: function, event, or constructor"
 },
                    inputs: {
                      type: "array",
                      description: "Input parameters",
                      items: {
                        type: "object",
                        properties: {
                          name: { 
                            type: "string",
                            description: "Parameter name"
 },
                          type: { 
                            type: "string",
                            description: "Parameter type"
 }
 }
 }
 }
 }
 }
 }
 }
 ]
 })

Editor options

options={{
 minimap: { enabled: false },
 lineNumbers: true,
 quickSuggestions: true, // Auto-completion
 autoIndent: true,
 automaticLayout: true,
 validate: true,
 folding: true,
 hover: {
 enabled: true,
 },
 suggest: {
 insertMode: "insert",
 showInlineDetails: true,
 showDetails: true,
 preview: true,
 previewMode: 'prefix',
 maxVisibleSuggestions: 12,
 },
 }}

Notes

  1. If you only need suggestions for specific editors, use fileMatch to restrict the scope; otherwise, it will trigger JSON suggestions in other editors.

Results

https://static.1991421.cn/2025/2025-03-19-170140.jpeg

https://static.1991421.cn/2025/2025-03-19-170221.jpeg

At the end

We now understand how to implement code suggestions for fixed JSON data types in Monaco editor.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover