11# jtd-infer
22
3- ` jtd-infer ` generates a JSON Typedef schema from example data.
3+ ` jtd-infer ` generates ("infers") a JSON Typedef schema from example data.
44
5- For high-level guidance on how to use this package, see [ "Inferring a JSON
6- Typedef Schema from Real Data"] [ jtd-jtd-infer ] in the JSON Typedef docs.
5+ ``` bash
6+ echo ' { "name": "Joe", "age": 42 }' | jtd-infer | jq
7+ ```
8+
9+ ``` json
10+ {
11+ "properties" : {
12+ "age" : {
13+ "type" : " uint8"
14+ },
15+ "name" : {
16+ "type" : " string"
17+ }
18+ }
19+ }
20+ ```
721
822## Installation
923
1024To install ` jtd-infer ` , you have a few options:
1125
12- ### Install with Homebrew
26+ ### Install on macOS
1327
14- This option is recommended if you're on macOS.
28+ You can install ` jtd-infer ` via Homebrew:
1529
1630``` bash
1731brew install jsontypedef/jsontypedef/jtd-infer
1832```
1933
34+ Alternatively, you can download and extract the binary yourself from
35+ ` x86_64-apple-darwin.zip ` in [ the latest release] [ latest ] . Because of Apple's
36+ quarantine system, you will need to run:
37+
38+ ``` bash
39+ xattr -d com.apple.quarantine path/to/jtd-infer
40+ ```
41+
42+ In order to be able to run the executable.
43+
44+ ### Install on Linux
45+
46+ Download and extract the binary from ` x86_64-unknown-linux-gnu.zip ` in [ the
47+ latest release] [ latest ] .
48+
49+ ### Install on Windows
50+
51+ Download and extract the binary from ` x86_64-pc-windows-gnu.zip ` in [ the latest
52+ release] [ latest ] . Runs on 64-bit MinGW for Windows 7+.
53+
2054### Install with Docker
2155
22- This option is recommended on non-Mac platforms, or if you're running
23- ` jtd-infer ` in some sort of script and you want to make sure that everyone
24- running the script uses the same version of ` jtd-infer ` .
56+ This option is recommended if you're running ` jtd-infer ` in some sort of script
57+ and you want to make sure that everyone running the script uses the same version
58+ of ` jtd-infer ` .
2559
2660``` bash
27- docker pull jsontypedef/jtd-tools
61+ docker pull jsontypedef/jtd-infer
2862```
2963
3064If you opt to use the Docker approach, you will need to change all invocations
@@ -37,31 +71,30 @@ jtd-infer [...]
3771To:
3872
3973``` bash
40- docker exec -it jsontypedef/jtd-tools /jtd-infer [...]
41- ```
42-
43- ### Install with Cargo
44-
45- This option is recommended if you already have ` cargo ` installed, or if you
46- would prefer to use a version of ` jtd-infer ` compiled on your machine:
74+ # To have jtd-infer read from STDIN, run it like so:
75+ docker exec -i jsontypedef/jtd-infer [...]
4776
48- ``` bash
49- cargo install jtd-infer
77+ # To have jtd-infer read from a file, run it as:
78+ docker run -v /path/to/file.json:/file.json -i jsontypedef/jtd-infer [...] file.json
79+ # or, if file.json is in your current directory:
80+ docker run -v $( pwd) /file.json:/file.json -i jsontypedef/jtd-infer [...] file.json
5081```
5182
5283## Usage
5384
54- > See the top of this README for links to high-level guidance on how to use
55- > ` jtd-infer ` .
85+ For high-level guidance on how to use ` jtd-infer ` , see [ "Inferring a JSON
86+ Typedef Schema from Real Data"] [ jtd-jtd-infer ] in the JSON Typedef website docs.
87+
88+ ### Basic Usage
5689
5790To invoke ` jtd-infer ` , you can either:
5891
59921 . Have it read from STDIN. This is the default behavior.
60932 . Have it read from a file. To do this, pass a file name as the last argument
6194 to ` jtd-infer ` .
6295
63- ` jtd-infer ` can read a _ sequence_ of JSON messages. So for example, if you have
64- a file like this in ` data.json ` :
96+ ` jtd-infer ` reads a _ sequence_ of JSON messages. So for example, if you have a
97+ file like this in ` data.json ` :
6598
6699``` json
67100{ "name" : " john doe" , "age" : 42 }
@@ -82,7 +115,7 @@ In both cases, you'd get this output:
82115{"properties" :{"name" :{"type" :" string" },"age" :{"type" :" uint8" }}}
83116```
84117
85- ## Advanced Usage: Providing Hints
118+ ### Advanced Usage: Providing Hints
86119
87120By default, ` jtd-infer ` will never output ` enum ` , ` values ` , or ` discriminator `
88121schemas. This is by design: by always being consistent with what it outputs,
@@ -102,7 +135,7 @@ As a corner-case, if you want to point to the *root* / top-level of your input,
102135then use the empty string as the path. See [ "Using
103136` --values-hint ` "] ( ##using---values-hint ) for an example of this.
104137
105- ### Using ` --enum-hint `
138+ #### Using ` --enum-hint `
106139
107140By default, strings are always inferred to be ` { "type": "string" } ` :
108141
@@ -126,7 +159,7 @@ echo '["foo", "bar", "baz"]' | jtd-infer --enum-hint=/-
126159{"elements" :{"enum" :[" bar" ," baz" ," foo" ]}}
127160```
128161
129- ### Using ` --values-hint `
162+ #### Using ` --values-hint `
130163
131164By default, objects are always assumed to be "structs", and ` jtd-infer ` will
132165generate ` properties ` / ` optionalProperties ` . For example:
@@ -151,7 +184,7 @@ echo '{"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, 9]}' | jtd-infer --values-hin
151184{"values" :{"elements" :{"type" :" uint8" }}}
152185```
153186
154- ### Using ` --discriminator-hint `
187+ #### Using ` --discriminator-hint `
155188
156189By default, objects are always assumed to be "structs", and ` jtd-infer ` will
157190generate ` properties ` / ` optionalProperties ` . For example:
@@ -197,3 +230,4 @@ echo '[{"type": "s", "value": "foo"},{"type": "n", "value": 3.14}]' | jtd-infer
197230```
198231
199232[ jtd-jtd-infer ] : https://jsontypedef.com/docs/tools/jtd-infer
233+ [ latest ] : https://github.com/jsontypedef/json-typedef-infer/releases/latest
0 commit comments