You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
:description: Instructions for parsing and building json within ESPHome.
6
+
:keywords: json
7
+
8
+
The ``json`` component enables ESPHome to work with JSON data in automations, sensors, and HTTP requests. This is particularly useful for:
9
+
10
+
- Processing API responses
11
+
- Sending structured data to external services
12
+
- Parsing configuration from JSON files
13
+
14
+
What is JSON?
15
+
16
+
JSON is a text syntax that facilitates structured data interchange between all programming languages. JSON
17
+
is a syntax of braces, brackets, colons, and commas that is useful in many contexts, profiles, and applications.
18
+
JSON stands for JavaScript Object Notation and was inspired by the object literals of JavaScript aka
19
+
ECMAScript as defined in the `ECMAScript Language Specification, Third Edition <https://ecma-international.org/wp-content/uploads/ECMA-404_2nd_edition_december_2017.pdf>`_ .
20
+
21
+
Example 1: Relatively complex JSON
22
+
23
+
.. code-block:: json
24
+
25
+
{
26
+
"first_name": "John",
27
+
"last_name": "Smith",
28
+
"is_alive": true,
29
+
"age": 27,
30
+
"address": {
31
+
"street_address": "21 2nd Street",
32
+
"city": "New York",
33
+
"state": "NY",
34
+
"postal_code": "10021-3100"
35
+
},
36
+
"phone_numbers": [
37
+
{
38
+
"type": "home",
39
+
"number": "212 555-1234"
40
+
},
41
+
{
42
+
"type": "office",
43
+
"number": "646 555-4567"
44
+
}
45
+
],
46
+
"children": [
47
+
"Catherine",
48
+
"Thomas",
49
+
"Trevor"
50
+
],
51
+
"spouse": null
52
+
}
53
+
54
+
Example 2: Simple JSON:
55
+
56
+
.. code-block:: json
57
+
58
+
{"key": 42.0, "greeting": "Hello World"}
59
+
60
+
61
+
Parsing JSON:
62
+
-------------
63
+
64
+
This example assumes that the server returns a response as a JSON object similar to this:
65
+
``{"status":"play","vol":"42","mute":"0"}``
66
+
67
+
68
+
If you want to retrieve the value for the ``vol`` key and assign it to a template ``sensor`` or ``number`` component
69
+
whose ``id`` is set to ``player_volume`` you can do this, but note that checking for the presence of the key will prevent difficult-to-read error messages:
You can build JSON in a lambda with a nested array like this:
96
+
97
+
.. code-block::
98
+
99
+
on_...:
100
+
- http_request.post:
101
+
url: https://esphome.io
102
+
json: |-
103
+
root["key"] = id(my_sensor).state;
104
+
root["greeting"] = "Hello World";
105
+
106
+
This will send::
107
+
``{"key": 42.0, "greeting": "Hello World"}``
108
+
109
+
110
+
Troubleshooting Errors:
111
+
-----------------------
112
+
A very common error when deserializing is:
113
+
114
+
.. code-block::
115
+
116
+
JSON parse error: InvalidInput
117
+
118
+
The software ESPHome uses does not provide particularly informative messages as to why, but
119
+
the people at ArduinoJson have created a `wonderful troubleshooter <https://arduinojson.org/troubleshooter>`__.
120
+
121
+
Another important resource is `JSONLint <https://jsonlint.com/>`__. It will help you determine if the JSON you are using is valid. It must be valid to work with ESPHome's deserializer and it probably needs to be valid for the destination, if you are sending it.
0 commit comments