JSON to Code

JSON to PropTypes

Paste a JSON sample. Get PropTypes types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.

Open the tool with PropTypes chosen

An example

This JSON:

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "joined": "2020-01-02T03:04:05Z",
  "tags": ["engineer", "writer"],
  "address": { "city": "London", "postcode": null },
  "scores": [9.5, 8, 10]
}

becomes this PropTypes:

// Example usage:
//
// import { MyShape } from ./myShape.js;
//
// class MyComponent extends React.Component {
//   //
// }
//
// MyComponent.propTypes = {
//   input: MyShape
// };

import PropTypes from "prop-types";

let _Root;
let _Address;
_Address = PropTypes.shape({
    "city": PropTypes.string,
    "postcode": PropTypes.any,
});
_Root = PropTypes.shape({
    "id": PropTypes.number,
    "name": PropTypes.string,
    "active": PropTypes.bool,
    "joined": PropTypes.string,
    "tags": PropTypes.arrayOf(PropTypes.string),
    "address": _Address,
    "scores": PropTypes.arrayOf(PropTypes.number),
});

export const Root = _Root;

With the reader and the writer

The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.

// Example usage:
//
// import { MyShape } from ./myShape.js;
//
// class MyComponent extends React.Component {
//   //
// }
//
// MyComponent.propTypes = {
//   input: MyShape
// };

import PropTypes from "prop-types";

let _Root;
let _Address;
_Address = PropTypes.shape({
    "city": PropTypes.string,
    "postcode": PropTypes.any,
});
_Root = PropTypes.shape({
    "id": PropTypes.number,
    "name": PropTypes.string,
    "active": PropTypes.bool,
    "joined": PropTypes.string,
    "tags": PropTypes.arrayOf(PropTypes.string),
    "address": _Address,
    "scores": PropTypes.arrayOf(PropTypes.number),
});

export const Root = _Root;

What you need in your project

`npm install prop-types`.

PropTypes check a React component property when the program runs, in development only.

A newer React project usually uses TypeScript instead. Pick this for a code base that already has PropTypes.

The trap to know about

PropTypes are removed in a production build, so they catch nothing for your users.

The options you can change

These belong to PropTypes, so the row of switches changes with the language.

OptionWhat it doesDefault
Which module system to useWhich module system to usecommon-js | es6

Questions

How do I turn JSON into PropTypes?

Paste your JSON into the box, choose PropTypes, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.

Does my JSON go to a server?

No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.

What do I need to add to my PropTypes project?

`npm install prop-types`.

How does it know which fields are optional?

From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.

Other languages

JSON to Zod JSON to Effect Schema JSON to JSON Schema All the guides

Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.

Open JSON to Code