npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

node-red-contrib-json-to-mqtt

v1.0.3

Published

Convert any JSON object to MQTT topic tree with configurable output formats

Readme

node-red-contrib-json-to-mqtt

A Node-RED node that converts any JSON object into an MQTT topic tree with configurable output formats. Perfect for bridging complex JSON APIs with MQTT-based systems.

Installation

Via NPM

npm install node-red-contrib-json-to-mqtt

Via Node-RED Palette Manager

  1. Open Node-RED
  2. Go to Menu → Manage palette
  3. Search for node-red-contrib-json-to-mqtt
  4. Click Install

Features

  • Universal JSON support - Handles any complex JSON structure, including nested objects and arrays
  • Flexible output formats - 4 different output modes to match your MQTT consumer requirements
  • Topic prefix - Optional prefix for all generated MQTT topics
  • Array indexing - Arrays automatically indexed in topic path (e.g., sensors/0, sensors/1)
  • Null value control - Choose whether to include or skip null values
  • Zero dependencies - Lightweight implementation with no external dependencies
  • Dynamic hints - In-editor help that updates based on selected format

Quick Start

  1. Drag the json-mqtt node from the Network palette into your flow
  2. Configure the Topic Prefix (optional, e.g., "weatherstation")
  3. Select your desired Output Format
  4. Connect the output to an mqtt out node
  5. Send a JSON object to the input

💡 Tip: Import example flows from the examples folder to see the node in action!

Output Formats

The node supports 4 different output formats to match your MQTT consumer requirements:

Input Example

{
  "home": {
    "sensors": [
      {
        "temp": 23.1,
        "hum": 67.5,
        "id": 1
      }
    ]
  }
}

1. Naked Value

Sends only the raw value without any wrapper. Best for simple numeric consumers.

Topic: home/sensors/0/temp
Payload: 23.1

Topic: home/sensors/0/hum
Payload: 67.5

Topic: home/sensors/0/id
Payload: 1

2. Value Object

Wraps the value in a JSON object with a value key. Useful for standardized consumers expecting this format.

Topic: home/sensors/0/temp
Payload: {"value": 23.1}

Topic: home/sensors/0/hum
Payload: {"value": 67.5}

3. Last Key

Creates a JSON object using the last key from the topic path. The payload contains the key-value pair.

Topic: home/sensors/0/temp
Payload: {"temp": 23.1}

Topic: home/sensors/0/hum
Payload: {"hum": 67.5}

4. Parent Object

Creates ONE message per object containing all primitive values. Stops at objects that contain only primitive values instead of creating individual messages for each field. This preserves the complete context of related values in a single message.

Topic: home/sensors/0
Payload: {"temp": 23.1, "hum": 67.5, "id": 1}

Note:

  • Creates a single message at the object level (not one per field)
  • Only primitive values (strings, numbers, booleans, null) are included in the payload
  • Nested objects and arrays are excluded
  • Useful when you want to keep all sensor readings together in one MQTT message

Configuration

Topic Prefix

Optional prefix added to all generated MQTT topics. For example, with prefix "home":

  • Without prefix: sensors/0/temp
  • With prefix: home/sensors/0/temp

Include Null Values

When checked, null values in the JSON will be published to MQTT topics. When unchecked (default), null values are skipped.

Output Format

Choose from 4 different output formats. The node provides dynamic hints in the editor that explain each format with examples.

How It Works

  1. Recursive traversal - The node recursively walks through your JSON structure
  2. Path building - Builds MQTT topic paths from JSON keys (e.g., data.sensors[0].tempdata/sensors/0/temp)
  3. Array handling - Arrays are automatically indexed (0, 1, 2, ...)
  4. Batch output - All messages are sent as an array in a single output, ready for mqtt out node

Real-World Example

Weather station API response:

{
  "station": {
    "id": "WS001",
    "readings": [
      {
        "temp": 23.1,
        "humidity": 67.5,
        "pressure": 1013.25
      }
    ]
  }
}

Configuration:

  • Prefix: weatherstation
  • Format: naked

Output (sent to MQTT):

Topic: weatherstation/station/id
Payload: "WS001"

Topic: weatherstation/station/readings/0/temp
Payload: 23.1

Topic: weatherstation/station/readings/0/humidity
Payload: 67.5

Topic: weatherstation/station/readings/0/pressure
Payload: 1013.25

Use Cases

  • 🌡️ IoT Sensor Data - Convert sensor readings from REST APIs to MQTT topics
  • 🏠 Smart Home Integration - Bridge JSON-based devices to MQTT home automation systems
  • 📊 Data Distribution - Fan out complex data structures to multiple MQTT consumers
  • 🔄 Protocol Translation - Convert between JSON APIs and MQTT-based systems

Troubleshooting

No messages output

  • Check that input payload is a valid JSON object
  • Verify "Include null values" setting if your data contains nulls

Too many messages

  • Consider using "Parent object" format to reduce message count
  • This groups related values into single messages at object level

Topic structure not as expected

  • Review your JSON structure - topics mirror the JSON hierarchy
  • Use Topic Prefix to add common prefix to all topics

Contributing

Issues and pull requests welcome at GitHub repository

License

MIT © SenseLog.com