{"id":18174520,"url":"https://github.com/jsontypedef/json-typedef-infer","last_synced_at":"2026-01-11T23:59:22.407Z","repository":{"id":43464207,"uuid":"256867550","full_name":"jsontypedef/json-typedef-infer","owner":"jsontypedef","description":" A CLI tool that generates JSON Typedef schemas from example data","archived":false,"fork":false,"pushed_at":"2023-07-16T14:48:10.000Z","size":48,"stargazers_count":54,"open_issues_count":11,"forks_count":10,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-02T16:08:40.846Z","etag":null,"topics":["inference","json","json-typedef"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jsontypedef.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-18T22:43:04.000Z","updated_at":"2024-10-02T17:48:32.000Z","dependencies_parsed_at":"2024-11-02T16:19:38.304Z","dependency_job_id":null,"html_url":"https://github.com/jsontypedef/json-typedef-infer","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsontypedef%2Fjson-typedef-infer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsontypedef%2Fjson-typedef-infer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsontypedef%2Fjson-typedef-infer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsontypedef%2Fjson-typedef-infer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jsontypedef","download_url":"https://codeload.github.com/jsontypedef/json-typedef-infer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246662368,"owners_count":20813737,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["inference","json","json-typedef"],"created_at":"2024-11-02T16:03:26.487Z","updated_at":"2026-01-11T23:59:22.402Z","avatar_url":"https://github.com/jsontypedef.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# jtd-infer: Generate JSON Typedef schemas from examples [![Crates.io](https://img.shields.io/crates/v/jtd_infer)](https://crates.io/crates/jtd_infer) [![Docs.rs](https://docs.rs/jtd-infer/badge.svg)](https://docs.rs/jtd_infer)\n\n[JSON Type Definition](https://jsontypedef.com), aka\n[RFC8927](https://tools.ietf.org/html/rfc8927), is an easy-to-learn,\nstandardized way to define a schema for JSON data. You can use JSON Typedef to\nportably validate data across programming languages, create dummy data, generate\ncode, and more.\n\n`jtd-infer` is a tool that generates (\"infers\") a JSON Typedef schema from\nexample data.\n\n```bash\necho '{ \"name\": \"Joe\", \"age\": 42 }' | jtd-infer | jq\n```\n\n```json\n{\n  \"properties\": {\n    \"age\": {\n      \"type\": \"uint8\"\n    },\n    \"name\": {\n      \"type\": \"string\"\n    }\n  }\n}\n```\n\n## Installation\n\nOn macOS, you can install `jtd-infer` via Homebrew:\n\n```bash\nbrew install jsontypedef/jsontypedef/jtd-infer\n```\n\nFor all other platforms, you can download and extract the binary yourself from\n[the latest release][latest]. You can also install using `cargo` by running:\n\n```bash\ncargo install jtd_infer\n```\n\n## Usage\n\nFor high-level guidance on how to use `jtd-infer`, see [\"Inferring a JSON\nTypedef Schema from Real Data\"][jtd-jtd-infer] in the JSON Typedef website docs.\n\n### Basic Usage\n\nTo invoke `jtd-infer`, you can either:\n\n1. Have it read from STDIN. This is the default behavior.\n2. Have it read from a file. To do this, pass a file name as the last argument\n   to `jtd-infer`.\n\n`jtd-infer` reads a _sequence_ of JSON messages. So for example, if you have a\nfile like this in `data.json`:\n\n```json\n{ \"name\": \"john doe\", \"age\": 42 }\n{ \"name\": \"jane doe\", \"age\": 45 }\n```\n\nYou can give it to `jtd-infer` in two ways:\n\n```bash\n# Both of these do the same thing.\ncat data.json | jtd-infer\njtd-infer data.json\n```\n\nIn both cases, you'd get this output:\n\n```json\n{\"properties\":{\"name\":{\"type\":\"string\"},\"age\":{\"type\":\"uint8\"}}}\n```\n\n### Changing the default number type\n\n\u003e ⚠️ This section is often important if you are retrofitting JSON Typedef to a\n\u003e JavaScript-based application.\n\nBy default, JSON Typedef will infer the most specific possible type for inputs.\nSo, for example, it will guess `uint8` if it sees a `12` in your input:\n\n```bash\necho \"12\" | jtd-infer\n```\n\n```json\n{\"type\":\"uint8\"}\n```\n\nHowever, if you're giving JSON Typedef a small sample set, or if you in practice\nhave data that is far smaller than the actual numerical datatypes your\napplication supports, then this behavior may be undesirable. For example, it's\ncommon for JavaScript-based applications to actually support `float64` for all\nnumeric inputs, because JavaScript numbers are IEEE double-precision floats.\n\nTo tell JSON Typedef to prefer a different type than the one it would normally\nguess, you can use `--default-number-type` to change its behavior. For example:\n\n```bash\n# JavaScript numbers are all float64s, and so it's pretty common for JavaScript\n# applications to not check if inputs are integers or within a particular range.\n#\n# If you don't want to make your JSON Typedef schema strict about decimal,\n# negative, or out of int range numbers, you can pass float64 as the default\n# number type.\necho \"12\" | jtd-infer --default-number-type=float64\n```\n\n```json\n{\"type\":\"float64\"}\n```\n\nAnother use-case is if you're writing an application that uses signed 32-bit\nints everywhere, and your example data simply never in practice has examples of\nnegative numbers or numbers too big for 8- or 16-bit numbers. You can achieve\nthat by using `int32` as your default number type:\n\n```bash\necho \"12\" | jtd-infer --default-number-type=int32\n```\n\n```json\n{\"type\":\"int32\"}\n```\n\nNote that `jtd-infer` will ignore your default if it doesn't match with the\ndata. For example, `int32` only works with whole numbers, so if a decimal number\nor a number too big for 32-bit signed integers comes in, it will fall back to\n`float64`:\n\n```bash\n# both of these output {\"type\":\"float64\"}\necho \"3.14\" | jtd-infer --default-number-type=int32\necho \"9999999999\" | jtd-infer --default-number-type=int32\n```\n\n### Advanced Usage: Providing Hints\n\nBy default, `jtd-infer` will never output `enum`, `values`, or `discriminator`\nschemas. This is by design: by always being consistent with what it outputs,\n`jtd-infer` is more predictable and reliable.\n\nIf you want `jtd-infer` to output an `enum`, `values`, or `discriminator`, you\ncan use the `--enum-hint`, `--values-hint`, and `--discriminator-hint` flags.\nYou can pass each of these flags multiple times.\n\nAll of the hint flags accept [JSON\nPointers](https://tools.ietf.org/html/rfc6901) as values. If you're used to the\nJavaScript-y syntax of referring to things as `$.foo.bar`, the equivalent JSON\nPointer is `/foo/bar`. `jtd-infer` treats `-` as a \"wildcard\". `/foo/-/bar` is\nequivalent to the JavaScript-y `$.foo.*.bar`.\n\nAs a corner-case, if you want to point to the *root* / top-level of your input,\nthen use the empty string as the path. See [\"Using\n`--values-hint`\"](##using---values-hint) for an example of this.\n\n#### Using `--enum-hint`\n\nBy default, strings are always inferred to be `{ \"type\": \"string\" }`:\n\n```bash\necho '[\"foo\", \"bar\", \"baz\"]' | jtd-infer\n```\n\n```json\n{\"elements\":{\"type\":\"string\"}}\n```\n\nBut you can instead have `jtd-infer` output an enum by providing a path to the\nstring you consider to be an enum. In this case, it's any element of the root of\nthe array -- the JSON Pointer for that is `/-`:\n\n```bash\necho '[\"foo\", \"bar\", \"baz\"]' | jtd-infer --enum-hint=/-\n```\n\n```json\n{\"elements\":{\"enum\":[\"bar\",\"baz\",\"foo\"]}}\n```\n\n#### Using `--values-hint`\n\nBy default, objects are always assumed to be \"structs\", and `jtd-infer` will\ngenerate `properties` / `optionalProperties`. For example:\n\n```bash\necho '{\"x\": [1, 2, 3], \"y\": [4, 5, 6], \"z\": [7, 8, 9]}' | jtd-infer\n```\n\n```json\n{\"properties\":{\"y\":{\"elements\":{\"type\":\"uint8\"}},\"z\":{\"elements\":{\"type\":\"uint8\"}},\"x\":{\"elements\":{\"type\":\"uint8\"}}}}\n```\n\nIf your data is more like a map / dictionary, pass a `values-hint` that points\nto the object that you want a `values` schema from. In this case, that's the\nroot-level object, which in JSON Pointer is just an empty string:\n\n```bash\necho '{\"x\": [1, 2, 3], \"y\": [4, 5, 6], \"z\": [7, 8, 9]}' | jtd-infer --values-hint=\n```\n\n```json\n{\"values\":{\"elements\":{\"type\":\"uint8\"}}}\n```\n\n#### Using `--discriminator-hint`\n\nBy default, objects are always assumed to be \"structs\", and `jtd-infer` will\ngenerate `properties` / `optionalProperties`. For example:\n\n```bash\necho '[{\"type\": \"s\", \"value\": \"foo\"},{\"type\": \"n\", \"value\": 3.14}]' | jtd-infer\n```\n\n```json\n{\"elements\":{\"properties\":{\"value\":{},\"type\":{\"type\":\"string\"}}}}\n```\n\nIf your data has a special \"type\" property that tells you what's in the rest of\nthe object, then use `--discriminator-hint` to point to that property.\n`jtd-infer` will output an appropriate `discriminator` schema instead:\n\n```bash\necho '[{\"type\": \"s\", \"value\": \"foo\"},{\"type\": \"n\", \"value\": 3.14}]' | jtd-infer --discriminator-hint=/-/type | jq\n```\n\n```json\n{\n  \"elements\": {\n    \"discriminator\": \"type\",\n    \"mapping\": {\n      \"s\": {\n        \"properties\": {\n          \"value\": {\n            \"type\": \"string\"\n          }\n        }\n      },\n      \"n\": {\n        \"properties\": {\n          \"value\": {\n            \"type\": \"float64\"\n          }\n        }\n      }\n    }\n  }\n}\n```\n\n[jtd-jtd-infer]: https://jsontypedef.com/docs/tools/jtd-infer\n[latest]: https://github.com/jsontypedef/json-typedef-infer/releases/latest\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsontypedef%2Fjson-typedef-infer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjsontypedef%2Fjson-typedef-infer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsontypedef%2Fjson-typedef-infer/lists"}