From 1d34362e3cb70a3fa87f9abba7dbc6e75cd5f8d4 Mon Sep 17 00:00:00 2001 From: Samantha Wang <32681364+sjwang90@users.noreply.github.com> Date: Thu, 22 Sep 2022 11:28:35 -0700 Subject: [PATCH] Update JSON Parsing Guide (#4465) * cleanup and edit JSON parsing doc * fix typo and working * Update content/influxdb/cloud/reference/json-parsing.md Co-authored-by: Scott Anderson * Update content/influxdb/cloud/reference/json-parsing.md Co-authored-by: Scott Anderson * Apply suggestions from code review Co-authored-by: Scott Anderson * Update content/influxdb/cloud/reference/json-parsing.md Co-authored-by: Scott Anderson * added data type column to tables Co-authored-by: Scott Anderson Co-authored-by: noramullen1 <42354779+noramullen1@users.noreply.github.com> --- .../influxdb/cloud/reference/json-parsing.md | 96 ++++++++++++++----- 1 file changed, 71 insertions(+), 25 deletions(-) diff --git a/content/influxdb/cloud/reference/json-parsing.md b/content/influxdb/cloud/reference/json-parsing.md index b620e5c68..ebd8b8436 100644 --- a/content/influxdb/cloud/reference/json-parsing.md +++ b/content/influxdb/cloud/reference/json-parsing.md @@ -10,45 +10,91 @@ influxdb/v2.0/tags: [mqtt] related: --- -Use the following examples to help you set up parsing rules for [native subscriptions](/influxdb/cloud/write-data/no-code/native-subscriptions). +Use the following examples to help you set up JSON parsing rules using [JSON Path](https://jsonpath.com/) +for [native subscriptions](/influxdb/cloud/write-data/no-code/native-subscriptions). All JSON paths start with a `$`. -## Example simple MQTT message in JSON format +## Example MQTT message in "flat" JSON format -```js +```json { "device_type":"temperature_sensor", "device_id":2036, "model_id":"KN24683", - "temperature":25.0, - "time":1653998899010000000, +"temperature":25.0, +"time":1653998899010000000, "error_state":"in_error" - } ``` -JSON paths start with a “$.” In the above example, all of the values are at the root level of the JSON, so the JSON paths for these elements are very simple: +With "flat" JSON, all values are at the root level (`$`) and are referenced with dot notation. -- Measurement: $.device_type -- Timestamp: $.time -- Tag: $.device_id -- Field 1: $.temperature -- Field 2: $.error_state +| InfluxDB Element | JSON Path | Data Type | Parsed Result | +| :--------------- | :-------------- | :-------- | :------------------- | +| Measurement | `$.device_type` | String | "temperature_sensor" | +| Timestamp | `$.time` | Timestamp | 1653998899010000000 | +| Tag | `$.device_id` | Integer | 2036 | +| Field 1 | `$.temperature` | Float | 25.0 | +| Field 2 | `$.error_state` | String | "in_error" | +## Example MQTT message with nested JSON objects -## Example nested MQTT message in JSON format - -```js +```json { -"device_information": { -"device_type":"temperature_sensor", -"device_id":2036, -"model_id":"KN24683" -}, - "temperature":25.0, - "time":165411795400000000, - "error_state":"in_error" + "device_information": { + "device_type":"temperature_sensor", + "device_id":2036, + "model_id":"KN24683" + }, + "temperature":25.0, + "time":165411795400000000, + "error_state":"in_error" } ``` -In this example, the JSON path to the measurement would be `$.device_information.device_type` -The JSON path to the tag would be `$device_information.device_id`. +| InfluxDB Element | JSON Path | Data Type | Parsed Result | +| :--------------- | :--------------------------------- | :-------- | :------------------- | +| Measurement | `$.device_information.device_type` | String | "temperature_sensor" | +| Timestamp | `$.time` | Timestamp | 1653998899010000000 | +| Tag | `$.device_information.device_id` | Integer | 2036 | +| Field 1 | `$.temperature` | Float | 25.0 | +| Field 2 | `$.error_state` | String | "in_error" | + +## Example MQTT message with JSON arrays +Currently, there is limited support for working with key/value pairs that are held within +a JSON array. Entire arrays cannot be loaded into a single field value, but if your messages +have a fixed number of values in the array being passed, you can specify an array index number +in your JSON path. + + +```json +{ + "device_information":{ + "device_type":"temperature_sensor", + "device_id":2309, + "model_id":"KN24683" + }, + "time":1653998899010000000, + "temperature":25.0, + "error_state":"in_error", + "errors_encountered":[ + { + "time_encountered":"2022:05:30:23:11", + "error_number":403 + }, + { + "time_encountered":"2022:06:01:12:15", + "error_number":404 + } + ] +} +``` + +| InfluxDB Element | JSON Path | Data Type | Parsed Result | +| :--------------- | :-------------------------------------- | :-------- | :------------------- | +| Measurement | `$.device_information.device_type` | String | "temperature_sensor" | +| Timestamp | `$.time` | Timestamp | 1653998899010000000 | +| Tag | `$.device_information.device_id` | Integer | 2036 | +| Field 1 | `$.temperature` | Float | 25.0 | +| Field 2 | `$.error_state` | String | "in_error" | +| Field 3 | `$.errors_encountered.[0].error_number` | Integer | 403 | +| Field 4 | `$.errors_encountered.[1].error_number` | Integer | 404 |