5 ms·
Show HN: A tool to Convert JSON schemas into TypeScript Deno classes
Hello all
To preface this is just something I've been making as a learning exercise, so all feedback is appreciated.
This is a tool that converts JSON schemas into TypeScript utility classes for use in Deno.
Automatic Type Generation: Typescript interfaces for the compressed and uncompressed versions of your data.
Compression & Decompression: Compress and decompress your data.
Validation: Built-in data validation using Ajv ensures your data adheres to the schema.
Reusability: Once generated, the utility classes can be used in other Deno projects.
It currently only supports a subset of JSON schema features. I'll eventually hope to expand it to support more features.
Reasons behind the project:
I have been working with OpenAI LLMs recently and I like to get my data output in structured JSON. I realised it's rather good at taking a schema where properties are compressed within an instruction, and then in return it return data based on the compressed schema. These utility classes provide help with the validation, decompression etc.
- ripperdoc 3y agoNice! JSON schemas are really useful, we use them a lot for code generation. Another library that does this for multiple languages is https://quicktype.io/ https://quicktype.io/ . It's great, but not so actively developed.
- code_biologist 3y agoI have the library demo linked from the homepage bookmarked: https://app.quicktype.io/ https://app.quicktype.io/ I use it every time I need to go from a pile of JSON to TypeScript types or zod declarations.
- mlajtos 3y agoWhy description isn't translated to Deno class?
- msp26 3y ago>Reasons behind the project: I have been working with OpenAI LLMs recently and I like to get my data output in structured JSON. I realised it's rather good at taking a schema where properties are compressed within an instruction, and then in return it return data based on the compressed schema. OpenAI Function Calling does JSON to Typescript conversion under the hood for functions so I'm not surprised it works well.
- verdverm 3y agoYou can pretty much make up any pseudo grammar like this one, which is a reduced JSON object that is close to CUE: https://github.com/hofstadter-io/hof/blob/_dev/flow/chat/prompts/dm.cue#L31 https://github.com/hofstadter-io/hof/blob/_dev/flow/chat/pro... No need to be formal or use a standard format, just need a pattern the LLM can fill or follow
- leigna 3y agoI have no idea what happens when I put data through these models and how they work. My thought was it may have been trained on so much JSON and JSON schema data, that simply providing a JSON schema and telling it the data it outputs must validate against the schema will produce good results. gpt-3.5-turbo and gpt-4 have worked superb at this so far and I'm excited to test with the new gpt-3.5-turbo-instruct model!
- verdverm 3y agoyes, if you finetune, you can get some preferred formats, see something like codellama. When using more generic model, you can do more generic things. One of the benefits of using a reduced syntax is reduced tokens, so the LLM can focus on the interesting parts while ignoring uninteresting " and , Let the model think more (and about the important parts)
- lvncelot 3y agoThis seems very interesting, thanks for sharing! I wonder if something similar could also be a usecase for Bun's macros, where certain code is executed at bundle time.[1] [1] https://bun.sh/docs/bundler/macros https://bun.sh/docs/bundler/macros
- leigna 3y agoThis looks intresting! I haven't looked too much at Bun yet, I've been quite obsessed with Deno. Maybe I should take a look. Coming from Node and JS, its been such a great experience working with it.
- bcherny 3y agoNice! Lots of tricky edge cases to do this right: there’s ambiguity in the JSON Schema spec, version-to-version changes, many popular community conventions that don’t adhere to the spec, etc. Feel free to check out the tests to understand some of these: https://github.com/bcherny/json-schema-to-typescript https://github.com/bcherny/json-schema-to-typescript (shameless plug — I have maintained this library for a number of years).
- leigna 3y agoGreat library - thank you for sharing. I'll take a look for sure. Yes it was rather tricky! It's been a great learning exercise for sure.
- michaelmior 3y agoAjv doesn't really do a great job at following the JSON Schema standard. I'd suggest taking a look at hyperjump. https://github.com/hyperjump-io/json-schema https://github.com/hyperjump-io/json-schema
- leigna 3y agoThanks for the suggestion - I'll take a look!
- Avlin67 3y agowhit ChatGPT all this kind of tools have no more values
- deepakarora3 3y agoNice! Talking of JSON schemas and validating JSON documents against schemas, for Java, I wrote unify-jdocs where I do not use JSON schemas but still do validations (I found them unwieldy to use and was looking for something simpler). You can find details here -> https://github.com/americanexpress/unify-jdocs https://github.com/americanexpress/unify-jdocs. Also, no POJOs / model classes, just reading and writing JSON paths in a single line of code. It's helped us tremendously in managing complexity in a very large internal project. I am hoping it helps others.
- leigna 3y agoI've not worked with Java but will certainly have a look as data handling etc I find very interesting. Thank you!