Change node JSON processing

Hi Guys, i am completely new to node-red and this is my first attempt to build a flow to connect API with mqtt. I am pulling the API response making the HTTP call and trying to grab the fields i need from the JSON response using change node.

Here is the expression i am applying to payload in change node:

{
  "idx": "648",
  "nvalue": 0,
  "svalue": payload.data.device_status.ts,
  "tttt": payload.data.device_status["temperature:0"].id,
  "xxx": payload.data.device_status["em:0"].total_act_power
}

Until "svalue" everything works well, but everything gets broken when i need to process payload which contains :0 in it.

Below is payload example and in green are the fields which were picked up by the expression i have.

".ts" field - works as it should
".id" field - picked the wrong one, i attemted to get the one above inside "temperature:0"
".total_act_power" - empty

it seems something is wrong with processing JSON lines containing :0" but i can't find out what is wrong. thanks in advance for your help!

Hi and welcome to the forum.

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path/value for any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

Use the copy path on the item to get the correct name.

Also JSONata expressions do not use square brackets for object properties, so it would be
payload.data.device_status."temperature:0".id Assuming the path was correct.

PS when sharing dat like objects it is best to copy the object using the copy value in the debug , and paste the object as text. Then we can be sure your path is correct and there is no ambiguity.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

1 Like

Great, thanks a lot for detailed reply! Now my expression works as it has to! :slight_smile:

1 Like