quicktype
generates strongly-typed models and serializers from JSON, JSON Schema, TypeScript, and GraphQL queries, making it a breeze to work with JSON type-safely in many programming languages.
Supported Inputs
TypeScript | GraphQL queries |
---|
Target Languages
Missing your favorite language? Please implement it!
Installation
There are many ways to use quicktype
. app.quicktype.io is the most powerful and complete UI. The web app also works offline and doesn't send your sample data over the Internet, so paste away!
For the best CLI, we recommend installing quicktype
globally via npm
:
npm install -g quicktype
Using quicktype
quicktype
echo '{ "name": "David" }' | quicktype -l csharp
echo '[1, 2, 3]' | quicktype -o ints.go
quicktype person.json -o Person.swift
quicktype \
--src person.json \
--src-lang json \
--lang swift \
--top-level Person \
--out Person.swift
quicktype ./blockchain -o blockchain-api.cpp
quicktype https://api.somewhere.com/data -o Data.java
Generating code from JSON schema
The recommended way to use quicktype
is to generate a JSON schema from sample data, review and edit the schema, commit the schema to your project repo, then generate code from the schema as part of your build process:
quicktype pokedex.json -l schema -o schema.json
quicktype -s schema schema.json -o src/ios/models.swift
quicktype -s schema schema.json -o src/android/Models.java
quicktype -s schema schema.json -o src/nodejs/Models.ts
Generating code from TypeScript (Experimental)
You can achieve a similar result by writing or generating a TypeScript file, then quicktyping it. TypeScript is a typed superset of JavaScript with simple, succinct syntax for defining types:
interface Person {
name: string;
nickname?: string;
luckyNumber: number;
}
You can use TypeScript just like JSON schema was used in the last example:
quicktype pokedex.json -o pokedex.ts --just-types
quicktype pokedex.ts -o src/ios/models.swift
Calling quicktype
from JavaScript
You can use quicktype
as a JavaScript function within node
or browsers. First add the quicktype-core
package:
$ npm install quicktype-core
In general, first you create an InputData
value with one or more JSON samples, JSON schemas, TypeScript sources, or other supported input types. Then you call quicktype
, passing that InputData
value and any options you want.
import {
quicktype,
InputData,
jsonInputForTargetLanguage,
JSONSchemaInput,
FetchingJSONSchemaStore
} from "quicktype-core";
async function quicktypeJSON(targetLanguage, typeName, jsonString) {
const jsonInput = jsonInputForTargetLanguage(targetLanguage);
await jsonInput.addSource({
name: typeName,
samples: [jsonString]
});
const inputData = new InputData();
inputData.addInput(jsonInput);
return await quicktype({
inputData,
lang: targetLanguage
});
}
async function quicktypeJSONSchema(targetLanguage, typeName, jsonSchemaString) {
const schemaInput = new JSONSchemaInput(new FetchingJSONSchemaStore());
await schemaInput.addSource({ name: typeName, schema: jsonSchemaString });
const inputData = new InputData();
inputData.addInput(schemaInput);
return await quicktype({
inputData,
lang: targetLanguage
});
}
async function main() {
const { lines: swiftPerson } = await quicktypeJSON("swift", "Person", jsonString);
console.log(swiftPerson.join("\n"));
const { lines: pythonPerson } = await quicktypeJSONSchema("python", "Person", jsonSchemaString);
console.log(pythonPerson.join("\n"));
}
main();
The argument to quicktype
is a complex object with many optional properties. Explore its definition to understand what options are allowed.
Contributing
quicktype
is Open Source and we love contributors! In fact, we have a list of issues that are low-priority for us, but for which we'd happily accept contributions. Support for new target languages is also strongly desired. If you'd like to contribute, need help with anything at all, or would just like to talk things over, come join us on Slack.
Setup, Build, Run
quicktype
is implemented in TypeScript and requires nodejs
and npm
to build and run.
First, install typescript
globally via npm
:
Clone this repo and do:
macOS / Linux
nvm use
npm install
script/quicktype
Windows
npm install --ignore-scripts
npm install -g typescript
tsc --project src/cli
node dist\cli\index.js
Edit
Install Visual Studio Code, open this
workspace, and install the recommended extensions:
code .
Live-reloading for quick feedback
When working on an output language, you'll want to view generated
output as you edit. Use npm start
to watch for changes and
recompile and rerun quicktype
for live feedback. For example, if you're
developing a new renderer for fortran
, you could use the following command to
rebuild and reinvoke quicktype
as you implement your renderer:
npm start -- "--lang fortran pokedex.json"
The command in quotes is passed to quicktype
, so you can render local .json
files, URLs, or add other options.
Test
npm run test
FIXTURE=golang npm test
FIXTURE=swift npm test -- pokedex.json
FIXTURE=swift npm test -- test/inputs/json/samples