home-assistant.io/source/_docs/mqtt/processing_json.markdown
Alok Saboo 1aca7b08cf Misc fixes: e.g. -> e.g., and proper case for Home Assistant (#4942)
* e.g. to e.g., and proper case for Home Assistant

* Instructions how to -> Instructions on how to
2018-03-17 20:20:37 +01:00

37 lines
1.2 KiB
Markdown

---
layout: page
title: "Processing JSON"
description: "Instructions on how to process the MQTT payload."
date: 2015-08-07 18:00
sidebar: true
comments: false
sharing: true
footer: true
logo: mqtt.png
---
The MQTT [switch](/components/switch.mqtt/) and [sensor](/components/sensor.mqtt/) platforms support processing JSON over MQTT messages and parsing them using JSONPath. JSONPath allows you to specify where in the JSON the value resides that you want to use. The following examples will always return the value `100`.
| JSONPath query | JSON |
| -------------- | ---- |
| `somekey` | `{ 'somekey': 100 }`
| `somekey[0]` | `{ 'somekey': [100] }`
| `somekey[0].value` | `{ 'somekey': [ { value: 100 } ] }`
To use this, add the following key to your `configuration.yaml`:
```yaml
switch:
platform: mqtt
state_format: 'json:somekey[0].value'
```
It is also possible to extract JSON values by using a value template:
```yaml
switch:
platform: mqtt
value_template: '{% raw %}{{ value_json.somekey[0].value }}{% endraw %}'
```
More information about the full JSONPath syntax can be found [in their documentation](https://github.com/kennknowles/python-jsonpath-rw#jsonpath-syntax).