Generate types
The JSON Explorer can turn the loaded JSON into type definitions that you paste into your code. It infers the shape of the data, including nested objects and array items, and renders it in the language you pick. Type generation works in the JSON Explorer extension and in the JSON Explorer inside Nouto.
Generate type definitions
Section titled “Generate type definitions”- Optional: select a node in the tree to generate types for that node only. The panel shows
Generating from:and the path of the selected node. With no node selected, or with the root selected, the panel uses the whole document. - Click Generate types (the interface icon) in the explorer toolbar. The Type Generator panel opens with TypeScript output.
- Click a language button at the top of the panel. The output updates immediately.
- Click the copy button in the panel header to copy the output to the clipboard.
The panel follows your selection while it is open, so you can click different nodes to generate types for each one.
![The Type Generator panel with TypeScript selected and Generating from: $[0].address, showing a Geo interface and a Root interface for the first user’s address](/_astro/generate-types.CbSv4Ox__Z1FD7x3.webp)
Output by language
Section titled “Output by language”| Language | Output |
|---|---|
| TypeScript | An interface for each object. A type alias for arrays and primitive values at the root. |
| Zod | An import { z } from 'zod' line, an exported schema for each field that holds an object, a rootSchema, and type Root = z.infer<typeof rootSchema>. |
| Rust | A pub struct for each object with #[derive(Debug, Serialize, Deserialize)]. Field names are converted to snake_case, with #[serde(rename = "...")] when the JSON key differs. |
| Go | A struct for each object with PascalCase field names and json:"key" tags. |
| Python | A @dataclass class for each object, with field names converted to snake_case. |
| JSON Schema | A schema with type, properties, items, and a required list. |
The output contains only the definitions. Add the imports your project needs, such as use serde::{Deserialize, Serialize}; for Rust or from dataclasses import dataclass for Python.
How types are inferred
Section titled “How types are inferred”The generator works from the values in the loaded JSON, so the output describes this sample of the data:
- The root type is named
Root. Nested types take the PascalCase form of their key, and array item types addItem, for exampleOrdersItem. - The type of an array’s items comes from the first item only. Make sure the first item is representative, or select a more representative node.
- An empty array becomes an array of an unknown type, such as
any[]in TypeScript orVec<serde_json::Value>in Rust. - A
nullvalue becomes a nullable or untyped field, such asany | nullin TypeScript orOption<serde_json::Value>in Rust. - Whole numbers and decimals map to different types where the language has them, for example
i64andf64in Rust, orintegerandnumberin JSON Schema. - The JSON Schema
requiredlist includes every key whose value is notnull.
