JSON to JavaScript Class Generator
CV-74Paste a JSON sample and press Convert to get ES classes or constructor functions. It infers the types from every item in your sample, and it runs in your browser.
Runs in your browser. Nothing is uploaded.
JSON
JavaScript
How to use it
- Paste a JSON sample into the input box, or open a .json file. Use a sample that shows every key you expect.
- Choose an ES class or a constructor function, and keep or drop the JSDoc comments.
- Press Convert, then copy the result or download it as a .js file.
- Build the model with new Root(JSON.parse(jsonText)).
How the classes are built
Each object becomes an ES class, or a constructor function if you choose one, whose constructor copies the keys from a parsed JSON object. Nested objects, and lists of objects, are turned into instances of their own classes. Keys that are valid identifiers keep their name; others become camelCase and are read with data["key"]. JSDoc comments give each type, with |null for null keys and a note on keys missing from some objects.
How the JSON is read
Every object in the sample is merged by key, so a key missing from some objects is marked optional and a null in some items marks the key nullable. If the document is a top-level array, its items are merged into one type named after the root name with Item added. The JSON is read with a parser that keeps number text exactly, so a big integer is not rounded before its type is chosen. Keys that are not valid JavaScript names, such as first-name, are renamed, and the original key is kept in the data["key"] read in the constructor.
What a sample cannot tell it
The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay strings, and an integer beyond 9007199254740991 is already rounded by JSON.parse before the class sees it. Check the result against the API documentation before you rely on it.
Questions people ask
How do I convert JSON to a JavaScript class?
Paste the JSON into the input box, choose your options and press Convert to get the classes. Build the model with new Root(JSON.parse(jsonText)). The conversion happens in your browser.
Class or constructor function for the JavaScript output?
The ES class is the modern choice and the default. Pick the constructor function if you target older environments or prefer the plain function style. Both copy the same keys and build nested objects into instances of their own classes.
How does the JavaScript output handle nested objects and arrays?
A nested object is built with new on its own class, and an array of objects is mapped into instances, so the result is a tree of class instances rather than plain data. Arrays of strings and numbers are kept as they are.
Does the JavaScript converter handle keys that are not valid names?
Yes. A key such as first-name is read with data["first-name"] and stored under the camelCase property firstName. Keys that are already valid identifiers keep their name. A key named constructor or __proto__ gets a trailing underscore.
What happens when my JSON starts with an array?
The tool reads every item and merges them into one type, so a key present in only some items becomes optional. The output names that type after the root name with Item added, and a comment at the top says the document is a list. The JavaScript classes are written from the parsed sample.
How are very large numbers in the JSON sample handled?
Number text is kept exactly while types are picked, so a value like 12345678901 gets a 64-bit type where the language has one, and an integer beyond 64 bits is flagged rather than silently rounded. JSON Schema only records integer or number. The JavaScript classes are written from the parsed sample.
About this tool
The classes are inferred from the JSON you paste, so they only know what the sample shows. Every object becomes an ES class (or a constructor function) whose constructor copies the keys from a parsed object, and nested objects and lists of objects are turned into instances of their own classes. Keys that are valid identifiers are used as property names; other keys are renamed to camelCase and read with data["key"]. JSDoc comments give each type, and an integer beyond 9007199254740991 is typed number even though JSON.parse rounds it. Missing keys stay undefined. There are no getters, setters or validation, and the output has no export line, so add one to match your module system. Everything runs in your browser; nothing is uploaded.