NodeRed has become a go-to tool for hobbyists and professionals who want to connect devices, APIs, and services without writing mountains of code. If you have ever worked with JSON data in NodeRed, you know the frustration of trying to pull out a specific value buried deep inside a nested structure. That is exactly what we are tackling today.
The Challenge: Nested JSON in NodeRed
JSON data rarely arrives in a flat, simple format. Real-world data often contains arrays nested within arrays, and keys scattered across multiple levels. Imagine receiving a payload from an IoT sensor or an API response that looks something like this: an object with keys like "direct" and "volume", where "volume" itself contains an array, and within that array sits another array holding the value you actually need.
This is a common scenario. You might be tracking fleet vehicles where "truck" represents a vehicle type, or monitoring data streams where "political" is a metric name. The problem is straightforward: how do you reliably grab those specific values without losing your mind?
NodeRed provides the tools, but you need to understand how they fit together.
The Required Nodes
You will need four core nodes for this extraction process. First, an Inject node to kick off the flow and provide your sample JSON data. Second, a JSON node that automatically parses the incoming string into a JavaScript object you can work with programmatically. Third, a Function node where you write the actual extraction logic using JavaScript. Finally, one or two Debug nodes to display the results so you can verify everything worked correctly.
The JSON node handles the heavy lifting of converting raw text into an object. Without it, you would be working with a string and manually parsing everything yourself. Let us save that headache.
Building the Extraction Flow
Start by dragging an Inject node onto your canvas and configuring it to output your sample JSON as a string. Click the button to paste your JSON directly into the configuration area. This gives you a reproducible test payload you can inject whenever you want to debug your flow.
Next, add the JSON node. It sits between your Inject node and the Function node. Set it to automatically convert the incoming string into a parsed JavaScript object.
Now comes the real work: the Function node. Here is where you extract the values you need. For a value like "truck" that sits inside the first element of the "volume" array, you access it with msg.payload.volume[0][1]. For a top-level key like "political", you simply use msg.payload.political.
Your Function node code creates a container object holding both extracted values. The key line is msg.payload.volume[0][1], which navigates through the nested structure to find the exact position of your target value. The first [0] selects the first array inside "volume". The second [1] selects the second element within that inner array, which happens to be the string "truck".
Our Analysis
This extraction pattern comes up constantly in automation projects. The specific indices you use depend entirely on your data structure, but the approach remains identical: navigate the object hierarchy, pull what you need, and return it in a clean format for the next node in your flow.
What makes NodeRed powerful here is that you are not locked into a single extraction method. You could use a Change node with JSONata expressions instead of writing JavaScript. You could use a Switch node to route different payloads based on extracted values. The Function node gives you maximum flexibility, but it is not your only option.
The real skill is understanding your data structure before you start writing extraction logic. Take time to examine your JSON visually or use Debug nodes to inspect intermediate steps. Building the flow becomes trivial once you know where your target values live.
A Practical Example
Say you are building a home automation flow that reads temperature data from a weather API. The API returns a nested JSON payload with multiple arrays. You need to extract the current temperature, which sits at payload.data[0].current.temp, and the humidity value from payload.data[0].current.humidity. You would follow the same pattern: use a JSON node to parse the incoming payload, then a Function node to extract both values into a clean object that your notification node can use to send you an alert when temperature crosses a threshold.
The steps remain constant regardless of your specific data. Parse, navigate, extract, and pass forward.
Why This Matters
Understanding how to reliably extract values from complex JSON structures unlocks the full potential of NodeRed for automation projects. Whether you are building IoT dashboards, integrating APIs, or processing sensor data, you will encounter nested JSON constantly. Getting comfortable with this extraction pattern means you spend less time fighting with data format issues and more time building useful automations.
Key takeaways
- Use the JSON node to automatically parse incoming string data into usable objects
- Navigate nested arrays using bracket notation like msg.payload.volume[0][1]
- Always inspect your payload structure with Debug nodes before writing extraction logic
- Multiple NodeRed nodes can achieve similar results; choose based on complexity and your comfort level
- Test your extraction with reproducible sample data using an Inject node
Frequently asked questions
Can I extract values without using a Function node?
Yes, you can use a Change node with JSONata expressions to extract values, which works well for simpler extractions.
How do I find the exact path to a nested value?
Use Debug nodes at intermediate steps to inspect msg.payload and see exactly how your data is structured.
What happens if the array index I am trying to access does not exist?
NodeRed will return undefined, which can cause errors in downstream nodes or unexpected behavior in your flow.
Can I extract multiple values in a single Function node?
Absolutely. Create a container object or array holding all the values you need and return it in a single message.
Does this approach work with all JSON structures?
Yes, the navigation pattern works for any nested JSON, though the specific indices and keys will differ based on your data.
Source: creatifwerks.com