-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add documentation for json component #4349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dbfc44b
cbf3cf5
c0f585b
755ed08
61ff6c9
6825d23
95be371
5aa4f4e
019c374
3527625
f4d6fb9
d7d6be1
eca5831
ae460c5
c207292
4d022ec
c03e2d5
e54a218
b1a742f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
json Component | ||
============== | ||
|
||
.. seo:: | ||
:description: Instructions for parsing and building json within ESPHome. | ||
:keywords: json | ||
|
||
The ``json`` component enables ESPHome to work with JSON data in automations, sensors, and HTTP requests. This is particularly useful for: | ||
|
||
- Processing API responses | ||
- Sending structured data to external services | ||
- Parsing configuration from JSON files | ||
|
||
What is JSON? | ||
|
||
JSON is a text syntax that facilitates structured data interchange between all programming languages. JSON | ||
is a syntax of braces, brackets, colons, and commas that is useful in many contexts, profiles, and applications. | ||
JSON stands for JavaScript Object Notation and was inspired by the object literals of JavaScript aka | ||
ECMAScript as defined in the `ECMAScript Language Specification, Third Edition <https://ecma-international.org/wp-content/uploads/ECMA-404_2nd_edition_december_2017.pdf>`_ . | ||
|
||
Example 1: Relatively complex JSON | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"first_name": "John", | ||
"last_name": "Smith", | ||
"is_alive": true, | ||
"age": 27, | ||
"address": { | ||
"street_address": "21 2nd Street", | ||
"city": "New York", | ||
"state": "NY", | ||
"postal_code": "10021-3100" | ||
}, | ||
"phone_numbers": [ | ||
{ | ||
"type": "home", | ||
"number": "212 555-1234" | ||
}, | ||
{ | ||
"type": "office", | ||
"number": "646 555-4567" | ||
} | ||
], | ||
"children": [ | ||
"Catherine", | ||
"Thomas", | ||
"Trevor" | ||
], | ||
"spouse": null | ||
} | ||
|
||
Example 2: Simple JSON: | ||
|
||
.. code-block:: json | ||
|
||
{"key": 42.0, "greeting": "Hello World"} | ||
|
||
|
||
Parsing JSON: | ||
------------- | ||
|
||
ChadMatsalla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This example assumes that the server returns a response as a JSON object similar to this: | ||
``{"status":"play","vol":"42","mute":"0"}`` | ||
|
||
|
||
If you want to retrieve the value for the ``vol`` key and assign it to a template ``sensor`` or ``number`` component | ||
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: | ||
|
||
.. code-block:: yaml | ||
|
||
on_...: | ||
- http_request.get: | ||
url: https://esphome.io | ||
capture_response: true | ||
on_response: | ||
then: | ||
- lambda: |- | ||
json::parse_json(body, [](JsonObject root) -> bool { | ||
if (root["vol"]) { | ||
id(player_volume).publish_state(root["vol"]); | ||
return true; | ||
} | ||
else { | ||
ESP_LOGD(TAG,"No 'vol' key in this json!"); | ||
return false; | ||
} | ||
}); | ||
|
||
|
||
Building JSON: | ||
-------------- | ||
|
||
You can build JSON in a lambda with a nested array like this: | ||
|
||
.. code-block:: | ||
|
||
on_...: | ||
- http_request.post: | ||
url: https://esphome.io | ||
json: |- | ||
root["key"] = id(my_sensor).state; | ||
root["greeting"] = "Hello World"; | ||
|
||
This will send:: | ||
``{"key": 42.0, "greeting": "Hello World"}`` | ||
|
||
|
||
Troubleshooting Errors: | ||
----------------------- | ||
A very common error when deserializing is: | ||
|
||
.. code-block:: | ||
|
||
JSON parse error: InvalidInput | ||
|
||
The software ESPHome uses does not provide particularly informative messages as to why, but | ||
the people at ArduinoJson have created a `wonderful troubleshooter <https://arduinojson.org/troubleshooter>`__. | ||
|
||
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. | ||
|
||
|
||
See Also | ||
-------- | ||
|
||
- :doc:`index` | ||
- :apiref:`http_request/http_request.h` | ||
- :apiref:`json/json_util.h` | ||
- `ArduinoJson <https://arduinojson.org/>` | ||
- :ghedit:`Edit` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,6 +172,7 @@ ESPHome-specific components or components supporting ESPHome device provisioning | |
Improv via BLE, components/esp32_improv, improv.svg, dark-invert | ||
Improv via Serial, components/improv_serial, improv.svg, dark-invert | ||
Interval, components/interval, description.svg, dark-invert | ||
JSON, components/json, json.svg, dark-invert | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Documentation path needs correction in index.rst The JSON component documentation exists at
🔗 Analysis chainLGTM! The JSON component entry is well-placed. The addition follows the correct format and maintains alphabetical ordering in the ESPHome Components section. Let's verify the referenced files exist: The previous results showed that while the icon exists at 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify the existence of the JSON component documentation and icon files
# Test 1: Check if the component documentation file exists
fd --glob "components/json.rst"
# Test 2: Check if the icon file exists
fd --glob "json.svg" images/
Length of output: 76 Script: #!/bin/bash
# Let's verify if the components/json directory exists and check its contents
fd --glob "components/json/*"
# Also check if there's a json.rst file directly under components
fd --glob "json.rst" components/
Length of output: 84 |
||
Script, components/script, description.svg, dark-invert | ||
|
||
ESPHome Configuration | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not entirely correct as a
0
value will return false in the above check