Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: A tool to Convert JSON schemas into TypeScript Deno classes (github.com/leigna)
43 points by leigna 11 months ago | hide | past | favorite | 16 comments
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.




Nice! 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 (shameless plug — I have maintained this library for a number of years).


Great 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.


Nice! 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/ . It's great, but not so actively developed.


I have the library demo linked from the homepage bookmarked: https://app.quicktype.io/ I use it every time I need to go from a pile of JSON to TypeScript types or zod declarations.


>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.


You 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/pro...

No need to be formal or use a standard format, just need a pattern the LLM can fill or follow


I 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!


yes, 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)


Nice! 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. 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.


I've not worked with Java but will certainly have a look as data handling etc I find very interesting. Thank you!


This 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


This 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.


Ajv 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


Thanks for the suggestion - I'll take a look!


Why description isn't translated to Deno class?


whit ChatGPT all this kind of tools have no more values




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: