json2struct is a tool that translates JSON into type definitions. It currently supports translating to TypeScript and Python.
The goal is for the definitions to be used as a starting point for the user, and not as a single source of truth.
The tool can either be run using npx
$ npx json2struct <REQUIRED_INPUT_FILE> [OPTIONAL_OUTPUT_FILE]
or be installed globally
$ npm i -g json2struct
$ json2struct <REQUIRED_INPUT_FILE> [OPTIONAL_OUTPUT_FILE]
To use the program pass the path to the json file $ json2struct example.json
.
By default the structure will be printed to stdout.
# example.json
{
"number_key": 1,
"string_key": "json2struct",
"boolean_key": true,
"array_key": [42],
"map_key": { "key": "value" }
}
$ npx json2struct example.json
json2struct: Converting example.json to typescript:
type GeneratedStruct = { array_key: Array<number>; boolean_key: boolean; map_key: { key: string }; number_key: number; string_key: string }
To write the sturcture to a file write the output file name after the input $ npx json2struct <REQUIRED_INPUT_FILE> [OPTIONAL_OUTPUT_FILE]
.
# example.json
{
"number_key": 1,
"string_key": "json2struct",
"boolean_key": true,
"array_key": [42],
"map_key": { "key": "value" }
}
$ npx json2struct example.json example.d.ts
Writes the following to the output file:
# example.d.ts
type GeneratedStruct = { array_key: Array<number>; boolean_key: boolean; map_key: { key: string }; number_key: number; string_key: string }
The default behavior is to append to the output file. This behavior can be overwritten by passing the option --overwrite
.
json2struct currently supports outputting to either TypeScript or Python. By default the conversion will be to TypeScript.
To use another language pass the --language
option.
# example.json
{
"number_key": 1,
"string_key": "json2struct",
"boolean_key": true,
"array_key": [42],
"map_key": { "key": "value" }
}
$ npx json2struct example.json example.d.ts --language python
Writes the following to the output file:
# example.py
from typing import List, TypedDict
class SubStruct1(TypedDict):
key: str
class GeneratedStruct(TypedDict):
array_key: List[int]
boolean_key: bool
map_key: SubStruct1
number_key: int
string_key: str
If json2struct isn't able to determine the type of a given element, it tries to convert it to the closest type possible.
By default empty arrays will be converted to:
Array<unknown>;
List[Any]
Hashmaps without keys will be converted to:
Record<string, unknown>;
Dict[Any, Any]