content/telegraf/v1/data_formats/input/json_v2.md
Use the json_v2 input data format to parse a JSON object or an array of objects into Telegraf metrics.
The parser supports GJSON Path Syntax for querying JSON.
To test your GJSON path, use GJSON Playground.
You can find multiple examples here in the Telegraf repository.
<!-- is this still true? {{% note %}} All JSON numbers are converted to float fields. JSON String are ignored unless specified in the `tag_key` or `json_string_fields` options. {{% /note %}} -->Configure this parser by describing the metric you want by defining the fields and tags from the input.
The configuration is divided into config sub-tables called field, tag, and object.
In the example below you can see all the possible configuration keys you can define for each config table.
In the sections that follow these configuration keys are defined in more detail.
[[inputs.file]]
urls = []
data_format = "json_v2"
[[inputs.file.json_v2]]
measurement_name = "" # A string that will become the new measurement name
measurement_name_path = "" # A string with valid GJSON path syntax, will override measurement_name
timestamp_path = "" # A string with valid GJSON path syntax to a valid timestamp (single value)
timestamp_format = "" # A string with a valid timestamp format (see below for possible values)
timestamp_timezone = "" # A string with with a valid timezone (see below for possible values)
[[inputs.file.json_v2.field]]
path = "" # A string with valid GJSON path syntax
rename = "new name" # A string with a new name for the tag key
type = "int" # A string specifying the type (int,uint,float,string,bool)
optional = false # true: suppress errors if configured path does not exist
[[inputs.file.json_v2.tag]]
path = "" # A string with valid GJSON path syntax
rename = "new name" # A string with a new name for the tag key
type = "float" # A string specifying the type (int,uint,float,string,bool)
optional = false # true: suppress errors if configured path does not exist
[[inputs.file.json_v2.object]]
path = "" # A string with valid GJSON path syntax
timestamp_key = "" # A JSON key (for a nested key, prepend the parent keys with underscores) to a valid timestamp
timestamp_format = "" # A string with a valid timestamp format (see below for possible values)
timestamp_timezone = "" # A string with with a valid timezone (see below for possible values)
disable_prepend_keys = false (or true, just not both)
included_keys = [] # List of JSON keys (for a nested key, prepend the parent keys with underscores) that should be only included in result
excluded_keys = [] # List of JSON keys (for a nested key, prepend the parent keys with underscores) that shouldn't be included in result
tags = [] # List of JSON keys (for a nested key, prepend the parent keys with underscores) to be a tag instead of a field
optional = false # true: suppress errors if configured path does not exist
[inputs.file.json_v2.object.renames] # A map of JSON keys (for a nested key, prepend the parent keys with underscores) with a new name for the tag key
key = "new name"
[inputs.file.json_v2.object.fields] # A map of JSON keys (for a nested key, prepend the parent keys with underscores) with a type (int,uint,float,string,bool)
key = "int"
measurement_name.unix, unix_ms, unix_us, unix_ns, or
the Go "reference time" which is defined to be the specific time:
Mon Jan 2 15:04:05 MST 2006America/New_York, to Local to utilize the system timezone, or to UTC.
Defaults to UTCThe following describes the high-level approach when parsing arrays and objects:
When handling nested arrays and objects, the rules above continue to apply as the parser creates metrics. When an object has multiple arrays as values, the arrays will become separate metrics containing only non-array values from the object. Below you can see an example of this behavior, with an input JSON containing an array of book objects that has a nested array of characters.
Example JSON:
{
"book": {
"title": "The Lord Of The Rings",
"chapters": [
"A Long-expected Party",
"The Shadow of the Past"
],
"author": "Tolkien",
"characters": [
{
"name": "Bilbo",
"species": "hobbit"
},
{
"name": "Frodo",
"species": "hobbit"
}
],
"random": [
1,
2
]
}
}
Example configuration:
[[inputs.file]]
files = ["./testdata/multiple_arrays_in_object/input.json"]
data_format = "json_v2"
[[inputs.file.json_v2]]
[[inputs.file.json_v2.object]]
path = "book"
tags = ["title"]
disable_prepend_keys = true
Expected metrics:
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",chapters="A Long-expected Party"
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",chapters="The Shadow of the Past"
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",name="Bilbo",species="hobbit"
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",name="Frodo",species="hobbit"
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",random=1
file,title=The\ Lord\ Of\ The\ Rings author="Tolkien",random=2
You can find more complicated examples under the folder [testdata][] in the telegraf repo.
For each field you have the option to define the types for each metric. The following rules are in place for this configuration:
The type values you can set:
int, bool, floats or strings (with valid numbers) can be converted to a int.uint, bool, floats or strings (with valid numbers) can be converted to a uint.string, any data can be formatted as a string.float, string values (with valid numbers) or integers can be converted to a float.bool, the string values "true" or "false" (regardless of capitalization) or the integer values 0 or 1 can be turned to a bool.