JSON to Flow
Paste a JSON sample. Get Flow 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 Flow 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 Flow:
// @flow
export type Root = {
id: number;
name: string;
active: boolean;
joined: Date;
tags: string[];
address: Address;
scores: number[];
};
export type Address = {
city: string;
postcode: null;
};
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.
// @flow
// To parse this data:
//
// const Convert = require("./file");
//
// const root = Convert.toRoot(json);
//
// These functions will throw an error if the JSON doesn't
// match the expected interface, even if the JSON is valid.
export type Root = {
id: number;
name: string;
active: boolean;
joined: Date;
tags: string[];
address: Address;
scores: number[];
};
export type Address = {
city: string;
postcode: null;
};
// Converts JSON strings to/from your types
// and asserts the results of JSON.parse at runtime
function toRoot(json: string): Root {
return cast(JSON.parse(json), r("Root"));
}
function rootToJson(value: Root): string {
return JSON.stringify(uncast(value, r("Root")), null, 2);
}
function invalidValue(typ: any, val: any, key: any, parent: any = '') {
const prettyTyp = prettyTypeName(typ);
const parentText = parent ? ` on ${parent}` : '';
const keyText = key ? ` for key "${key}"` : '';
throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`);
}
function prettyTypeName(typ: any): string {
if (Array.isArray(typ)) {
and 164 more lines. Open the tool to see the whole file.
What you need in your project
Flow.
Flow is the other type checker for JavaScript. The output is a type alias for each object, with the reader that checks at run time.
The trap to know about
A Flow type, like a TypeScript one, is gone when the program runs.
The options you can change
These belong to Flow, so the row of switches changes with the language.
| Option | What it does | Default |
|---|---|---|
| Check at run time | Verify JSON.parse results at runtime | true |
| Types only | Interfaces only | false |
| Tidy the names | Transform property names to be JavaScripty | false |
| Name the unions | Explicitly name unions | false |
| A union, not an enum | Use union type instead of enum | true |
| type, not interface | Use types instead of interfaces | false |
| A string, not a one-value enum | Use string instead of enum for string enums with single value | false |
| Readonly members | Use readonly type members | false |
Questions
How do I turn JSON into Flow?
Paste your JSON into the box, choose Flow, 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 Flow project?
Flow.
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
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