Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1.2k
Generate types and converters from JSON, Schema, and GraphQL
License
glideapps/quicktype
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
quicktype generates strongly-typed models and serializers from JSON, JSON Schema, TypeScript, andGraphQL queries, making it a breeze to work with JSON type-safely in many programming languages.
- Try
quicktypein your browser. - Read'A first look at quicktype' for more introduction.
- If you have any questions, check out theFAQ first.
| JSON | JSON API URLs | JSON Schema |
|---|
| TypeScript | GraphQL queries |
|---|
| Ruby | JavaScript | Flow | Rust | Kotlin |
|---|
| Dart | Python | C# | Go | C++ |
|---|
| Java | Scala | TypeScript | Swift | Objective-C | Elm |
|---|
| JSON Schema | Pike | Prop-Types | Haskell | PHP |
|---|
Missing your favorite language? Please implement it!
There are many ways to usequicktype.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 installingquicktype globally vianpm:
npm install -g quicktype
# Run quicktype without arguments for help and optionsquicktype# quicktype a simple JSON object in C#echo'{ "name": "David" }'| quicktype -l csharp# quicktype a top-level array and save as Go sourceecho'[1, 2, 3]'| quicktype -o ints.go# quicktype a sample JSON file in Swiftquicktype person.json -o Person.swift# A verbose way to do the same thingquicktype \ --src person.json \ --src-lang json \ --lang swift \ --top-level Person \ --out Person.swift# quicktype a directory of samples as a C++ program# Suppose ./blockchain is a directory with files:# latest-block.json transactions.json marketcap.jsonquicktype ./blockchain -o blockchain-api.cpp# quicktype a live JSON API as a Java programquicktype https://api.somewhere.com/data -o Data.java
The recommended way to usequicktype 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:
# First, infer a JSON schema from a sample.quicktype pokedex.json -l schema -o schema.json# Review the schema, make changes,# and commit it to your project repo.# Finally, generate model code from schema in your# build process for whatever languages you need:quicktype -s schema schema.json -o src/ios/models.swiftquicktype -s schema schema.json -o src/android/Models.javaquicktype -s schema schema.json -o src/nodejs/Models.ts# All of these models will serialize to and from the same# JSON, so different programs in your stack can communicate# seamlessly.
You can achieve a similar result by writing or generating aTypeScript file, then quicktyping it. TypeScript is a typed superset of JavaScript with simple, succinct syntax for defining types:
interfacePerson{name:string;nickname?:string;// an optional propertyluckyNumber:number;}
You can use TypeScript just like JSON schema was used in the last example:
# First, infer a TypeScript file from a sample (or just write one!)quicktype pokedex.json -o pokedex.ts --just-types# Review the TypeScript, make changes, etc.quicktype pokedex.ts -o src/ios/models.swift
You can usequicktype as a JavaScript function withinnode or browsers. First add thequicktype-core package:
$ npm install quicktype-core
In general, first you create anInputData value with one or more JSON samples, JSON schemas, TypeScript sources, or other supported input types. Then you callquicktype, passing thatInputData value and any options you want.
import{quicktype,InputData,jsonInputForTargetLanguage,JSONSchemaInput,FetchingJSONSchemaStore}from"quicktype-core";asyncfunctionquicktypeJSON(targetLanguage,typeName,jsonString){constjsonInput=jsonInputForTargetLanguage(targetLanguage);// We could add multiple samples for the same desired// type, or many sources for other types. Here we're// just making one type from one piece of sample JSON.awaitjsonInput.addSource({name:typeName,samples:[jsonString]});constinputData=newInputData();inputData.addInput(jsonInput);returnawaitquicktype({ inputData,lang:targetLanguage});}asyncfunctionquicktypeJSONSchema(targetLanguage,typeName,jsonSchemaString){constschemaInput=newJSONSchemaInput(newFetchingJSONSchemaStore());// We could add multiple schemas for multiple types,// but here we're just making one type from JSON schema.awaitschemaInput.addSource({name:typeName,schema:jsonSchemaString});constinputData=newInputData();inputData.addInput(schemaInput);returnawaitquicktype({ inputData,lang:targetLanguage});}asyncfunctionmain(){const{lines:swiftPerson}=awaitquicktypeJSON("swift","Person",jsonString);console.log(swiftPerson.join("\n"));const{lines:pythonPerson}=awaitquicktypeJSONSchema("python","Person",jsonSchemaString);console.log(pythonPerson.join("\n"));}main();
The argument toquicktype is a complex object with many optional properties.Explore its definition to understand what options are allowed.
Quicktype supports creating your own custom languages and rendering output, you can extend existing classes or create your own to be using by thequicktype function.
Check outthis guide for more info.
quicktype isOpen Source and we love contributors! In fact, we have alist 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, comejoin us on Slack.
quicktype is implemented in TypeScript and requiresnodejs andnpm to build and run.
First, installtypescript globally vianpm:
Clone this repo and do:
nvm usenpm installscript/quicktype# rebuild (slow) and run (fast)npm install --ignore-scripts# Install dependenciesnpm install -g typescript# Install typescript globallytsc --project src/cli# Rebuildnode dist\cli\index.js# Run
InstallVisual Studio Code, open thisworkspace, and install the recommended extensions:
code.# opens in VS Code
When working on an output language, you'll want to view generatedoutput as you edit. Usenpm start to watch for changes andrecompile and rerunquicktype for live feedback. For example, if you'redeveloping a new renderer forfortran, you could use the following command torebuild and reinvokequicktype as you implement your renderer:
npm start --"--lang fortran pokedex.json"The command in quotes is passed toquicktype, so you can render local.jsonfiles, URLs, or add other options.
# Run full test suitenpm runtest# Test a specific language (see test/languages.ts)FIXTURE=golang npmtest# Test a single sample or directoryFIXTURE=swift npmtest -- pokedex.jsonFIXTURE=swift npmtest -- test/inputs/json/samples
About
Generate types and converters from JSON, Schema, and GraphQL
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.