Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions components/http_request.rst
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ can assign values to keys by using the ``root["KEY_NAME"] = VALUE;`` syntax as s

GET values from a JSON body response
************************************
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:


This example assumes that the server returns a response as a JSON object similar to this:
``{"status":"play","vol":"42","mute":"0"}``
Expand All @@ -263,14 +267,20 @@ whose ``id`` is set to ``player_volume``:
then:
- lambda: |-
json::parse_json(body, [](JsonObject root) -> bool {
id(player_volume).publish_state(root["vol"]);
return true;
if (root["vol"]) {
id(player_volume).publish_state(root["vol"]);
return true;
}
else {
ESP_LOGD(TAG,"No 'vol' key in this json!");
Copy link
Member

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

return false;
}
});


See Also
--------

- :doc:`index`
- :apiref:`http_request/http_request.h`
- :doc:`/components/json`
- :ghedit:`Edit`
132 changes: 132 additions & 0 deletions components/json.rst
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:
-------------

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`

1 change: 1 addition & 0 deletions images/json.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 components/json.rst, but the index entry references it as components/json. Update the path to match the actual file location:

  • Line 175: Change JSON, components/json, json.svg, dark-invert to JSON, components/json.rst, json.svg, dark-invert
🔗 Analysis chain

LGTM! 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 images/json.svg, we didn't find the exact path components/json.rst. Let's check both the directory and direct file to understand the documentation structure better.

🏁 Scripts executed

The 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
Expand Down