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 <sanderson@users.noreply.github.com> * Update content/influxdb/cloud/reference/json-parsing.md Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com> * Update content/influxdb/cloud/reference/json-parsing.md Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com> * added data type column to tables Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com> Co-authored-by: noramullen1 <42354779+noramullen1@users.noreply.github.com>pull/4480/head
parent
07421d073b
commit
1d34362e3c
|
|
@ -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 |
|
||||
|
|
|
|||
Loading…
Reference in New Issue