1---
2last_modified_on: "2020-07-13"
3component_title: "JSON Parser"
4description: "The Vector `json_parser` transform accepts and outputs `log` events, allowing you to parse a log field value as JSON."
5event_types: ["log"]
6function_category: "parse"
7issues_url: https://github.com/timberio/vector/issues?q=is%3Aopen+is%3Aissue+label%3A%22transform%3A+json_parser%22
8operating_systems: ["Linux","MacOS","Windows"]
9sidebar_label: "json_parser|[\"log\"]"
10source_url: https://github.com/timberio/vector/tree/master/src/transforms/json_parser.rs
11status: "prod-ready"
12title: "JSON Parser Transform"
13unsupported_operating_systems: []
14---
15
16import Fields from '@site/src/components/Fields';
17import Field from '@site/src/components/Field';
18import Tabs from '@theme/Tabs';
19import TabItem from '@theme/TabItem';
20
21The Vector `json_parser` transform
22accepts and outputs [`log`][docs.data-model.log] events, allowing you to parse
23a log field value as JSON.
24
25<!--
26     THIS FILE IS AUTOGENERATED!
27
28     To make changes please edit the template located at:
29
30     website/docs/reference/transforms/json_parser.md.erb
31-->
32
33## Configuration
34
35<Tabs
36  block={true}
37  defaultValue="common"
38  values={[{"label":"Common","value":"common"},{"label":"Advanced","value":"advanced"}]}>
39<TabItem value="common">
40
41```toml title="vector.toml"
42[transforms.my_transform_id]
43  type = "json_parser" # required
44  inputs = ["my-source-or-transform-id"] # required
45  drop_field = true # optional, default
46  drop_invalid = true # required
47  field = "message" # optional, default
48```
49
50</TabItem>
51<TabItem value="advanced">
52
53```toml title="vector.toml"
54[transforms.my_transform_id]
55  type = "json_parser" # required
56  inputs = ["my-source-or-transform-id"] # required
57  drop_field = true # optional, default
58  drop_invalid = true # required
59  field = "message" # optional, default
60  overwrite_target = false # optional, default
61  target_field = "root_field" # optional, no default
62```
63
64</TabItem>
65</Tabs>
66
67<Fields filters={true}>
68<Field
69  common={true}
70  defaultValue={true}
71  enumValues={null}
72  examples={[true,false]}
73  groups={[]}
74  name={"drop_field"}
75  path={null}
76  relevantWhen={null}
77  required={false}
78  templateable={false}
79  type={"bool"}
80  unit={null}
81  warnings={[]}
82  >
83
84### drop_field
85
86If the specified [`field`](#field) should be dropped (removed) after parsing. If parsing
87fails, the field will not be removed, irrespective of this setting.
88
89
90
91</Field>
92<Field
93  common={true}
94  defaultValue={null}
95  enumValues={null}
96  examples={[true]}
97  groups={[]}
98  name={"drop_invalid"}
99  path={null}
100  relevantWhen={null}
101  required={true}
102  templateable={false}
103  type={"bool"}
104  unit={null}
105  warnings={[]}
106  >
107
108### drop_invalid
109
110If `true` events with invalid JSON will be dropped, otherwise the event will be
111kept and passed through.
112 See [Invalid JSON](#invalid-json) for more info.
113
114
115</Field>
116<Field
117  common={true}
118  defaultValue={"message"}
119  enumValues={null}
120  examples={["message","parent.child","array[0]"]}
121  groups={[]}
122  name={"field"}
123  path={null}
124  relevantWhen={null}
125  required={false}
126  templateable={false}
127  type={"string"}
128  unit={null}
129  warnings={[]}
130  >
131
132### field
133
134The log field to decode as JSON. Must be a `string` value type.
135 See [Field Notation Syntax](#field-notation-syntax) and [Invalid JSON](#invalid-json) for more info.
136
137
138</Field>
139<Field
140  common={false}
141  defaultValue={false}
142  enumValues={null}
143  examples={[false,true]}
144  groups={[]}
145  name={"overwrite_target"}
146  path={null}
147  relevantWhen={null}
148  required={false}
149  templateable={false}
150  type={"bool"}
151  unit={null}
152  warnings={[]}
153  >
154
155### overwrite_target
156
157If [`target_field`](#target_field) is set and the log contains a field of the same name as the
158target, it will only be overwritten if this is set to `true`.
159
160
161
162</Field>
163<Field
164  common={false}
165  defaultValue={null}
166  enumValues={null}
167  examples={["root_field","parent.child"]}
168  groups={[]}
169  name={"target_field"}
170  path={null}
171  relevantWhen={null}
172  required={false}
173  templateable={false}
174  type={"string"}
175  unit={null}
176  warnings={[]}
177  >
178
179### target_field
180
181If this setting is present, the parsed JSON will be inserted into the log as a
182sub-object with this name. If a field with the same name already exists, the
183parser will fail and produce an error.
184 See [Field Notation Syntax](#field-notation-syntax) for more info.
185
186
187</Field>
188</Fields>
189
190## Examples
191
192<Tabs
193  block={true}
194  defaultValue="simple"
195  select={false}
196  values={[{"label":"Simple","value":"simple"},{"label":"Wrapped","value":"wrapped"}]}>
197
198<TabItem value="simple">
199
200Given the following log event:
201
202```javascript
203{
204  "message": "{"key": "value"}"
205}
206```
207
208You can parse the JSON with:
209
210```toml
211[transforms.json]
212  inputs = ["<source_id>"]
213  type   = "json_parser"
214  field  = "message"
215```
216
217This would produce the following event as output:
218
219```javascript
220{
221  "key": "value"
222}
223```
224
225By default, Vector drops fields after parsing them via the [`drop_field`](#drop_field)
226option.
227
228</TabItem>
229
230<TabItem value="wrapped">
231
232It is possible to chain `json_parser` transforms to effectively "unwrap"
233nested JSON documents. For example, give this log event:
234
235```javascript
236{
237  "message": "{"parent": "{"child": "value2"}"}"
238}
239```
240
241You could unwrap the JSON with the following transforms:
242
243```toml
244[transforms.root_json]
245  inputs = ["<source_id>"]
246  type   = "json_parser"
247  field  = "message"
248
249[transforms.parent_json]
250  inputs = ["root_json"]
251  type   = "json_parser"
252  field  = "parent"
253
254[transforms.child_json]
255  inputs = ["parent_json"]
256  type   = "json_parser"
257  field  = "child"
258```
259
260This would produce the following event as output:
261
262```javascript
263{
264  "child": "value2"
265}
266```
267
268By default, Vector drops fields after parsing them via the [`drop_field`](#drop_field)
269option.
270
271</TabItem>
272</Tabs>
273
274## How It Works
275
276### Chaining / Unwrapping
277
278Please see the [Examples section](#examples).
279
280### Complex Processing
281
282If you encounter limitations with the `json_parser`
283transform then we recommend using a [runtime transform][urls.vector_programmable_transforms].
284These transforms are designed for complex processing and give you the power of
285full programming runtime.
286
287### Environment Variables
288
289Environment variables are supported through all of Vector's configuration.
290Simply add `${MY_ENV_VAR}` in your Vector configuration file and the variable
291will be replaced before being evaluated.
292
293You can learn more in the
294[Environment Variables][docs.configuration#environment-variables] section.
295
296### Field Notation Syntax
297
298The [`field`](#field) and [`target_field`](#target_field) options
299support [Vector's field notation syntax][docs.reference.field-path-notation],
300enabling access to root-level, nested, and array field values. For example:
301
302```toml title="vector.toml"
303[transforms.my_json_parser_transform_id]
304  # ...
305  field = "message"
306  field = "parent.child"
307  field = "array[0]"
308  # ...
309```
310
311You can learn more about Vector's field notation in the
312[field notation reference][docs.reference.field-path-notation].
313
314### Invalid JSON
315
316If the value for the specified [`field`](#field) is not valid JSON you can control keep
317or discard the event with the [`drop_invalid`](#drop_invalid) option. Setting it to `true` will
318discard the event and drop it entirely. Setting it to `false` will keep the
319event and pass it through. Note that passing through the event could cause
320problems and violate assumptions about the structure of your event.
321
322### Merge Conflicts
323
324#### Key conflicts
325
326Any key present in the decoded JSON will override existin keys in the event.
327
328#### Object conflicts
329
330If the decoded JSON includes nested fields it will be _deep_ merged into the
331event. For example, given the following event:
332
333```javascript
334{
335  "message": "{\"parent\": {\"child2\": \"value2\"}}",
336  "parent": {
337    "child1": "value1"
338  }
339}
340```
341
342Parsing the `"message"` field would result the following structure:
343
344```javascript
345{
346  "parent": {
347    "child1": "value1",
348    "child2": "value2"
349  }
350}
351```
352
353Notice that the `parent.child1` key was preserved.
354
355[docs.configuration#environment-variables]: /docs/setup/configuration/#environment-variables
356[docs.data-model.log]: /docs/about/data-model/log/
357[docs.reference.field-path-notation]: /docs/reference/field-path-notation/
358[urls.vector_programmable_transforms]: https://vector.dev/components/?functions%5B%5D=program
359