Implementing JSON Schema-based Intelligent Suggestions in Monaco Editor

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.