Create a JSON editor
In this tutorial, we will create a JSON editor Extension with React and the react-json-view package. Our application will display a JSON file’s content and allow users to edit, add or delete properties directly from the editor. When a user finishes editing the JSON, the file will automatically update with the changes.Prerequisites
This tutorial assumes that you have a basic knowledge and understanding of web development and React.Set up your Replit App
- Fork the Replit React Extension Template.
- Install the
react-json-viewpackage withnpm install --force react-json-view. The package uses React 17 as a peer dependency but works fine with React 18 as well.
Configure the manifest file
Configure the title and description inpublic/extension.json (the Extension manifest file).
fileHandlers property to extension.json and provide a handler for JSON files. This tells Replit that your extension handles a particular file pattern using a page provided by your extension at the handler path. In this case, the handler is /, meaning that Replit shows the page at the root as the handler for all .json files
Build the JSON editor
Import the following dependencies insrc/App.jsx.
App function and a state variable path, which will point to the JSON file your Extension will render.
Initialize the Handshake
Initialize the handshake and derive thestatus and error properties from the useReplit hook within the App function.
The status property is an enumerated value indicating whether the handshake connection with Replit is loading, ready, or has resulted in an error.
Get the File Path
Use theuseReplitEffect hook and set the path state to the extensionPort’s file path. This will set the path state once the handshake between Replit and your Extension has been established.
Create the File Watcher
You can easily create a file watcher with theuseWatchTextFile hook. Call the hook, pass in the file path, and derive content and writeChange from it.
Reflecting file contents
Create aparsedContent React Memo that returns the content file value as parsed JSON. If there is an error parsing it, return null instead.
The React useMemo hook caches a result based on an array of dependencies between re-renders to improve performance. Caching reduces the amount of computing required in a process, ultimately improving performance.
Handle file changes
Create a function which handles changes from the react-json-view editor component. Theupdated_src property passed into this function is a JSON object.
Stringify the JSON object and then write it to the JSON file using the writeChange function.
Finally, update the setContent state to reflect the contents of the file.
Build the UI
It’s time to start building the UI. First, handle loading and error states.

path and content are valid strings, render the editor. If not, tell the user to select a file.


Style your Extension
Right now, the Extension has barely any styles applied to it. To make it look more polished, paste the following intoApp.css:
Your Extension is now complete! Install it, press the kebab menu on a JSON file in the file tree and then select “Open with JSON Editor” to start editing your JSON files with ease. See full solution.