Skip to content
Advertisement

How do I format JSON code in Monaco Editor with API?

I am working with monaco editor aka the VS Code engine in a web project.

I am using it to allow users to edit some JSON that has a JSON Schema set, to help give some auto-completion.

When they save their changes and wish to re-edit their work, the JSON that I load back into the editor is converted to a string but this renders the code out on a single line and I would much prefer the JSON to be prettier as if the user right clicks and uses the Format Document command from the context menu or keyboard shortcut etc..

So this

JavaScript

Would become this

JavaScript

Current attempt

I have tried the following but it feels very hacky to use a timeout to wait/guess when the content has loaded

JavaScript
JavaScript

Does anyone have a better idea or solution to this please?

Advertisement

Answer

JavaScript
JavaScript

Thanks to https://stackoverflow.com/users/1378051/dalie for reminding me about the extra arguments to JSON.stringify

Answer

Use the tab character for the space argument
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

const jsonModel = monaco.editor.createModel(JSON.stringify(jsonCode, null, 't'), "json", modelUri);

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement