Skip to content

bombsimon/jtd-infer-go

Repository files navigation

JSON typedef infer

Go Reference Build and test

This is a port of json-typedef-infer for Go. The reason for porting this is that I was in need of JTD inference from code and not as a CLI tool.

For more information about JSON Typedef and its RFC and how to use different kind of hints see json-typedef-infer.

Usage

See examples directory for runnable examples and how to infer JTD.

schema := NewInferrer(WithoutHints()).
    Infer("my-string").
    IntoSchema()
// {
//   "type": "string"
// }

If you have multiple rows of objects or lists as strings you can pass them to the shorthand function InferStrings.

rows := []string{
    `{"name":"Joe", "age": 52, "something_optional": true, "something_nullable": 1.1}`,
    `{"name":"Jane", "age": 48, "something_nullable": null}`,
}
schema := InferStrings(rows, WithoutHints()).IntoSchema()
// {
//   "properties": {
//     "age": {
//       "type": "uint8"
//     },
//     "name": {
//       "type": "string"
//     },
//     "something_nullable": {
//       "nullable": true,
//       "type": "float64"
//     }
//   },
//   "optionalProperties": {
//     "something_optional": {
//       "type": "boolean"
//     }
//   }
// }