1
1
# jtd-infer
2
2
3
- ` jtd-infer ` generates a JSON Typedef schema from example data.
3
+ ` jtd-infer ` generates ("infers") a JSON Typedef schema from example data.
4
4
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
+ ```
7
21
8
22
## Installation
9
23
10
24
To install ` jtd-infer ` , you have a few options:
11
25
12
- ### Install with Homebrew
26
+ ### Install on macOS
13
27
14
- This option is recommended if you're on macOS.
28
+ You can install ` jtd-infer ` via Homebrew:
15
29
16
30
``` bash
17
31
brew install jsontypedef/jsontypedef/jtd-infer
18
32
```
19
33
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
+
20
54
### Install with Docker
21
55
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 ` .
25
59
26
60
``` bash
27
- docker pull jsontypedef/jtd-tools
61
+ docker pull jsontypedef/jtd-infer
28
62
```
29
63
30
64
If you opt to use the Docker approach, you will need to change all invocations
@@ -37,31 +71,30 @@ jtd-infer [...]
37
71
To:
38
72
39
73
``` 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 [...]
47
76
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
50
81
```
51
82
52
83
## Usage
53
84
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
56
89
57
90
To invoke ` jtd-infer ` , you can either:
58
91
59
92
1 . Have it read from STDIN. This is the default behavior.
60
93
2 . Have it read from a file. To do this, pass a file name as the last argument
61
94
to ` jtd-infer ` .
62
95
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 ` :
65
98
66
99
``` json
67
100
{ "name" : " john doe" , "age" : 42 }
@@ -82,7 +115,7 @@ In both cases, you'd get this output:
82
115
{"properties" :{"name" :{"type" :" string" },"age" :{"type" :" uint8" }}}
83
116
```
84
117
85
- ## Advanced Usage: Providing Hints
118
+ ### Advanced Usage: Providing Hints
86
119
87
120
By default, ` jtd-infer ` will never output ` enum ` , ` values ` , or ` discriminator `
88
121
schemas. 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,
102
135
then use the empty string as the path. See [ "Using
103
136
` --values-hint ` "] ( ##using---values-hint ) for an example of this.
104
137
105
- ### Using ` --enum-hint `
138
+ #### Using ` --enum-hint `
106
139
107
140
By default, strings are always inferred to be ` { "type": "string" } ` :
108
141
@@ -126,7 +159,7 @@ echo '["foo", "bar", "baz"]' | jtd-infer --enum-hint=/-
126
159
{"elements" :{"enum" :[" bar" ," baz" ," foo" ]}}
127
160
```
128
161
129
- ### Using ` --values-hint `
162
+ #### Using ` --values-hint `
130
163
131
164
By default, objects are always assumed to be "structs", and ` jtd-infer ` will
132
165
generate ` 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
151
184
{"values" :{"elements" :{"type" :" uint8" }}}
152
185
```
153
186
154
- ### Using ` --discriminator-hint `
187
+ #### Using ` --discriminator-hint `
155
188
156
189
By default, objects are always assumed to be "structs", and ` jtd-infer ` will
157
190
generate ` properties ` / ` optionalProperties ` . For example:
@@ -197,3 +230,4 @@ echo '[{"type": "s", "value": "foo"},{"type": "n", "value": 3.14}]' | jtd-infer
197
230
```
198
231
199
232
[ jtd-jtd-infer ] : https://jsontypedef.com/docs/tools/jtd-infer
233
+ [ latest ] : https://github.com/jsontypedef/json-typedef-infer/releases/latest
0 commit comments