Convert JSON to TypeScript Types or YAML
A sample JSON response can save typing when you need an initial TypeScript interface or YAML configuration. Conversion preserves a sample's visible structure; it cannot tell you every value the source API may return later.
Draft TypeScript from a sample
Paste valid JSON into the JSON Formatter and choose its TypeScript output. Review nested objects and arrays. A field present in one example may still be optional in production, and a number in one sample may later be null. Compare generated types with the API's official schema or multiple representative responses.
Convert to YAML carefully
The same tool can convert supported JSON to YAML. Check arrays, strings that resemble numbers or booleans, and keys with unusual characters. Then validate the YAML with the application that will consume it. A syntactically valid file can still be an invalid deployment configuration.
If the input contains credentials or personal data, remove them before pasting, even when a tool processes data in the browser. Keep a clean source sample and test the output in your project before committing it.
Check what a sample cannot reveal
A generated interface reflects the JSON you pasted. If a response sample contains an empty array, it does not tell the generator the array element type. If one response omits an optional field, the tool cannot infer its full schema. For a union of several response shapes, compare multiple samples or use the service's published schema, then edit the type by hand. Generated types do not validate data at runtime; add validation where untrusted responses enter the application.
For YAML, quote or inspect values that a parser may interpret as numbers, booleans, or dates. Keep comments and target-specific schema requirements in your final configuration. Run the consuming tool's own validation command before deployment, because structural conversion alone cannot guarantee valid semantics.
Frequently Asked Questions (FAQ)
Can one JSON sample reveal every optional field? No. Compare the API's schema or multiple representative responses, then edit the generated type.
Does a TypeScript interface validate a response at runtime? No. Interfaces disappear after compilation. Validate untrusted data when your application receives it.
Can valid YAML still fail deployment? Yes. The target system may require different keys, values, or versions. Run its own validation or dry-run command before applying the file.
Loading comments...