Is this a real JSON string? I'm not able to extract a value!

Dear all,
I'm trying to solve thi issue but also searching in other forum Topic, I'm not able to solve:
I have a simple JSON String from a simple HTTP Request node with "GET:"

"{"IJFVIUEHIUVHEURGUERGIUEWRG":{"final_balance":524535,"n_tx":430,"total_received":453443}}"

I just want to extract the "final_balance" value, but I'm not able!
Can someone help me?

PS: After this, I just want to compare the new value with the previous value, (that I have to save in some way?) and get an alarm if the value is changed!
Thanks in advance.

Hi!

You could try https://jsonlint.com to check the JSON string. It says "Valid JSON".

What happens when you feed that into a JSON node? If it complains then post what you see in a debug node before the JSON node.

[Edit] If the JSON node does not complain, then what do you get out of it?

To compare current to previous value, you need to store the current value in a global/flow variable. Before storing the current value, you need to compare the current value with the variable - noting that the first time you do that, you won't have a variable because you haven't stored it yet. To get round that, you can store a dummy value to it on Node-RED startup (using the inject node). Though if you are using a function node to do the comparison, you can simply provide a default dummy value.

You can use a Filter node for that. Set it to test msg.payload.IJFVIUEHIUVHEURGUERGIUEWRG.final_balance and only pass on a value if it has changed, or if it has changed by a particular amount if that is what is required.

The Json is complaying.

But I 'mot able to write the correct code into the FUNCTION node to extract only the "final_balance" value: plese see the attached:

If the property name changes then you could use Object.values().
e.g.

msg.payload = Object.values(msg.payload)[0].final_balance
return msg;

To only pass on change , you can use a switch node, set to does not equal previous value.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.