|
| 1 | +# jtd-infer |
| 2 | + |
| 3 | +`jtd-infer` generates a JSON Typedef schema from example data. |
| 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. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +To install `jtd-infer`, you have a few options: |
| 11 | + |
| 12 | +### Install with Homebrew |
| 13 | + |
| 14 | +This option is recommended if you're on macOS. |
| 15 | + |
| 16 | +```bash |
| 17 | +brew install jsontypedef/jsontypedef/jtd-infer |
| 18 | +``` |
| 19 | + |
| 20 | +### Install with Docker |
| 21 | + |
| 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`. |
| 25 | + |
| 26 | +```bash |
| 27 | +docker pull jsontypedef/jtd-tools |
| 28 | +``` |
| 29 | + |
| 30 | +If you opt to use the Docker approach, you will need to change all invocations |
| 31 | +of `jtd-infer` in this README from: |
| 32 | + |
| 33 | +```bash |
| 34 | +jtd-infer [...] |
| 35 | +``` |
| 36 | + |
| 37 | +To: |
| 38 | + |
| 39 | +```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: |
| 47 | + |
| 48 | +```bash |
| 49 | +cargo install jtd-infer |
| 50 | +``` |
| 51 | + |
| 52 | +## Usage |
| 53 | + |
| 54 | +> See the top of this README for links to high-level guidance on how to use |
| 55 | +> `jtd-infer`. |
| 56 | +
|
| 57 | +To invoke `jtd-infer`, you can either: |
| 58 | + |
| 59 | +1. Have it read from STDIN. This is the default behavior. |
| 60 | +2. Have it read from a file. To do this, pass a file name as the last argument |
| 61 | + to `jtd-infer`. |
| 62 | + |
| 63 | +`jtd-infer` can read a _sequence_ of JSON messages. So for example, if you have |
| 64 | +a file like this in `data.json`: |
| 65 | + |
| 66 | +```json |
| 67 | +{ "name": "john doe", "age": 42 } |
| 68 | +{ "name": "jane doe", "age": 45 } |
| 69 | +``` |
| 70 | + |
| 71 | +You can give it to `jtd-infer` in two ways: |
| 72 | + |
| 73 | +```bash |
| 74 | +# Both of these do the same thing. |
| 75 | +cat data.json | jtd-infer |
| 76 | +jtd-infer data.json |
| 77 | +``` |
| 78 | + |
| 79 | +In both cases, you'd get this output: |
| 80 | + |
| 81 | +```json |
| 82 | +{"properties":{"name":{"type":"string"},"age":{"type":"uint8"}}} |
| 83 | +``` |
| 84 | + |
| 85 | +## Advanced Usage: Providing Hints |
| 86 | + |
| 87 | +By default, `jtd-infer` will never output `enum`, `values`, or `discriminator` |
| 88 | +schemas. This is by design: by always being consistent with what it outputs, |
| 89 | +`jtd-infer` is more predictable and reliable. |
| 90 | + |
| 91 | +If you want `jtd-infer` to output an `enum`, `values`, or `discriminator`, you |
| 92 | +can use the `--enum-hint`, `--values-hint`, and `--discriminator-hint` flags. |
| 93 | +You can pass each of these flags multiple times. |
| 94 | + |
| 95 | +All of the hint flags accept [JSON |
| 96 | +Pointers](https://tools.ietf.org/html/rfc6901) as values. If you're used to the |
| 97 | +JavaScript-y syntax of referring to things as `$.foo.bar`, the equivalent JSON |
| 98 | +Pointer is `/foo/bar`. `jtd-infer` treats `-` as a "wildcard". `/foo/-/bar` is |
| 99 | +equivalent to the JavaScript-y `$.foo.*.bar`. |
| 100 | + |
| 101 | +As a corner-case, if you want to point to the *root* / top-level of your input, |
| 102 | +then use the empty string as the path. See ["Using |
| 103 | +`--values-hint`"](##using---values-hint) for an example of this. |
| 104 | + |
| 105 | +### Using `--enum-hint` |
| 106 | + |
| 107 | +By default, strings are always inferred to be `{ "type": "string" }`: |
| 108 | + |
| 109 | +```bash |
| 110 | +echo '["foo", "bar", "baz"]' | jtd-infer |
| 111 | +``` |
| 112 | + |
| 113 | +```json |
| 114 | +{"elements":{"type":"string"}} |
| 115 | +``` |
| 116 | + |
| 117 | +But you can instead have `jtd-infer` output an enum by providing a path to the |
| 118 | +string you consider to be an enum. In this case, it's any element of the root of |
| 119 | +the array -- the JSON Pointer for that is `/-`: |
| 120 | + |
| 121 | +```bash |
| 122 | +echo '["foo", "bar", "baz"]' | jtd-infer --enum-hint=/- |
| 123 | +``` |
| 124 | + |
| 125 | +```json |
| 126 | +{"elements":{"enum":["bar","baz","foo"]}} |
| 127 | +``` |
| 128 | + |
| 129 | +### Using `--values-hint` |
| 130 | + |
| 131 | +By default, objects are always assumed to be "structs", and `jtd-infer` will |
| 132 | +generate `properties` / `optionalProperties`. For example: |
| 133 | + |
| 134 | +```bash |
| 135 | +echo '{"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, 9]}' | jtd-infer |
| 136 | +``` |
| 137 | + |
| 138 | +```json |
| 139 | +{"properties":{"y":{"elements":{"type":"uint8"}},"z":{"elements":{"type":"uint8"}},"x":{"elements":{"type":"uint8"}}}} |
| 140 | +``` |
| 141 | + |
| 142 | +If your data is more like a map / dictionary, pass a `values-hint` that points |
| 143 | +to the object that you want a `values` schema from. In this case, that's the |
| 144 | +root-level object, which in JSON Pointer is just an empty string: |
| 145 | + |
| 146 | +```bash |
| 147 | +echo '{"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, 9]}' | jtd-infer --values-hint= |
| 148 | +``` |
| 149 | + |
| 150 | +```json |
| 151 | +{"values":{"elements":{"type":"uint8"}}} |
| 152 | +``` |
| 153 | + |
| 154 | +### Using `--discriminator-hint` |
| 155 | + |
| 156 | +By default, objects are always assumed to be "structs", and `jtd-infer` will |
| 157 | +generate `properties` / `optionalProperties`. For example: |
| 158 | + |
| 159 | +```bash |
| 160 | +echo '[{"type": "s", "value": "foo"},{"type": "n", "value": 3.14}]' | jtd-infer |
| 161 | +``` |
| 162 | + |
| 163 | +```json |
| 164 | +{"elements":{"properties":{"value":{},"type":{"type":"string"}}}} |
| 165 | +``` |
| 166 | + |
| 167 | +If your data has a special "type" property that tells you what's in the rest of |
| 168 | +the object, then use `--discriminator-hint` to point to that property. |
| 169 | +`jtd-infer` will output an appropriate `discriminator` schema instead: |
| 170 | + |
| 171 | +```bash |
| 172 | +echo '[{"type": "s", "value": "foo"},{"type": "n", "value": 3.14}]' | jtd-infer --discriminator-hint=/-/type | jq |
| 173 | +``` |
| 174 | + |
| 175 | +```json |
| 176 | +{ |
| 177 | + "elements": { |
| 178 | + "discriminator": "type", |
| 179 | + "mapping": { |
| 180 | + "s": { |
| 181 | + "properties": { |
| 182 | + "value": { |
| 183 | + "type": "string" |
| 184 | + } |
| 185 | + } |
| 186 | + }, |
| 187 | + "n": { |
| 188 | + "properties": { |
| 189 | + "value": { |
| 190 | + "type": "float64" |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | +} |
| 197 | +``` |
| 198 | + |
| 199 | +[jtd-jtd-infer]: https://jsontypedef.com/docs/tools/jtd-infer |
0 commit comments