Getting just the json from editor.get() #314
-
Hi, I previously used the JsonEditor and I've migrated over to the Svelte one. One issue I seem stuck on is just getting the json in the editor. Previously, regardless of the mode, editor.get() would return the json but with this one, depending on the mode i.e. text or tree, it either returns text (which needs to be parsed) or json. I simply want a way to just get the containing json in the editor, regardless of mode. Any easy way of doing this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can use the utility function You can also write your own utility function: function contentToJSON(content) {
return content.json !== undefined
? content.json
: JSON.parse(content.text) // Note: can throw an error
} |
Beta Was this translation helpful? Give feedback.
-
Thank you, that's perfect. I hadn't seen this utility function and as I'm just bringing in the editor via cdn, I'll stick with the custom utility function to expand on it. Appreciate the really fast response. |
Beta Was this translation helpful? Give feedback.
You can use the utility function
toJSONContent(content).json
to get the contents as a JSON object. Note that this function can throw an exception when the contents of the editor are no valid JSON (for example when the user is halfway typing in text mode, or the document is empty).You can also write your own utility function: