Skip to content

Nested JSON

kreso975 edited this page Nov 26, 2025 · 5 revisions

Understanding Nested JSON in homebridge-http-sensors-switches Plugin

When working with Homebridge homebridge-http-sensors-switches plugin, you’ll need to parse data from external sources such as APIs or URLs. These data sources usually return information in JSON format.

For novice users, JSON (JavaScript Object Notation) may look intimidating at first, especially when values are nested inside multiple levels. This guide explains what nested JSON is, how to read it, and how it is used in your Homebridge plugin.



πŸ”Ή What is JSON?

  • JSON is a lightweight data format used to store and exchange information.
  • It is structured as key-value pairs.
  • Keys are always strings, while values can be:
    • Strings
    • Numbers
    • Booleans (true / false)
    • Arrays ([ ])
    • Objects ({ })

Example of a simple JSON object:

{
  "name": "Lamp",
  "status": "on",
  "brightness": 80
}

πŸ”Ή What is Nested JSON?

Nested JSON means that values themselves can be objects or arrays, creating multiple levels of hierarchy.

Example:

{
  "device": {
    "name": "Lamp",
    "settings": {
      "status": "on",
      "brightness": 80
    }
  }
}

Here:

  • device is an object.
  • Inside device, there is another object called settings.
  • Inside settings, we have keys like status and brightness.

πŸ”Ή Accessing Nested JSON

To access nested values, you use dot notation:

  • device.name β†’ "Lamp"
  • device.settings.status β†’ "on"
  • device.settings.brightness β†’ 80

This is what we mean by level2.level1.paramName:

  • level2 β†’ device
  • level1 β†’ settings
  • paramName β†’ status or brightness

πŸ”Ή Practical Example in Plugin Config

Homebridge homebridge-http-sensors-switches plugin need to map JSON values from APIs. For example, if your API returns nested JSON, you must tell the plugin exactly where to find the value. Suppose your plugin configuration requires you to define where to find the temperature value. You might write:

Example API response:

{
  "sensor": {
    "temperature": {
      "current": 22.5,
      "unit": "C"
    }
  }
}

To read the current temperature:

  • It will navigate through sensor β†’ temperature β†’ current.
  • Path: sensor.temperature.current
  • Value: 22.5

πŸ”Ή Common Patterns

  • Single level: paramName
  • Two levels: level1.paramName
  • Three levels: level2.level1.paramName

πŸ”Ή Tips for Users

  • Identify the exact path to the value you need.
  • Use dot notation in your plugin config to point to the correct value.