Create a JSON Forms App
This section describes how you can integrate JSON Forms into a React app from scratch. Alternatively you can also clone the seed app.
We'll use create-react-app to scaffold a basic React application which we'll use as a starting point. If you didn't install
create-react-app
yet, please do so now before continuing.Let's now create a basic application with:
If you want to use typescript within your project, use the following command instead:
Scaffolding may take a couple of moments. Once it's finished, switch to your newly created app.
Install JSON Forms and the material renderer set. We'll use an example from JSON Forms in order to obtain a JSON schema, a corresponding UI schema and a data instance to be rendered. You don't need to install the
@jsonforms/examples
package if you plan to supply your own schema in the following:Switch to the
src
directory and openApp.js
(App.tsx
when using typescript) with an editor of your choice. Let's add a couple of imports first:The
person
import is necessary for importing the person example while@jsonforms/material-renderers
imports the Material UI based renderer set and respective cells.Now let's define the variables that are crucial for JSON Forms to work, that is,
data
,schema
anduischema
. As previously mentioned, we are using the person example from JSON Forms here:These variables are defined as follows:
- Demo
- Schema
- UI Schema
- Data
schema.json{"type": "object","properties": {"name": {"type": "string","minLength": 3,"description": "Please enter your name"},"vegetarian": {"type": "boolean"},"birthDate": {"type": "string","format": "date"},"nationality": {"type": "string","enum": ["DE","IT","JP","US","RU","Other"]},"personalData": {"type": "object","properties": {"age": {"type": "integer","description": "Please enter your age."},"height": {"type": "number"},"drivingSkill": {"type": "number","maximum": 10,"minimum": 1,"default": 7}},"required": ["age","height"]},"occupation": {"type": "string"},"postalCode": {"type": "string","maxLength": 5}},"required": ["occupation","nationality"]}uischema.json{"type": "VerticalLayout","elements": [{"type": "HorizontalLayout","elements": [{"type": "Control","scope": "#/properties/name"},{"type": "Control","scope": "#/properties/personalData/properties/age"},{"type": "Control","scope": "#/properties/birthDate"}]},{"type": "Label","text": "Additional Information"},{"type": "HorizontalLayout","elements": [{"type": "Control","scope": "#/properties/personalData/properties/height"},{"type": "Control","scope": "#/properties/nationality"},{"type": "Control","scope": "#/properties/occupation","suggestion": ["Accountant","Engineer","Freelancer","Journalism","Physician","Student","Teacher","Other"]}]}]}{"name": "John Doe","vegetarian": false,"birthDate": "1985-06-02","personalData": {"age": 34},"postalCode": "12345"}Now import the
JsonForms
component from@jsonforms/react
. Delete theheader
element and replace it with theJsonForms
element to get a form rendered:The optional
onChange
handler demonstrates how you can listen to data and validation changes in JSON Forms.Now you have a basic form up and running! Take a look at our seed app for more examples.