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

**URL:** <https://discourse.nodered.org/t/is-this-a-real-json-string-im-not-able-to-extract-a-value/59747>\
**Category:** General\
**Tags:** function-node\
**Created:** [12 March 2022 10:13 UTC](https://discourse.nodered.org/t/is-this-a-real-json-string-im-not-able-to-extract-a-value/59747 "2022-03-12T10:13:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lumanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lumanga/32/58334_2.png) [@lumanga](https://discourse.nodered.org/u/lumanga)\
**Post date:** [12 March 2022 10:13 UTC](https://discourse.nodered.org/t/is-this-a-real-json-string-im-not-able-to-extract-a-value/59747/1 "2022-03-12T10:13:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rei\_vilo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rei_vilo/32/162_2.png) [@rei\_vilo](https://discourse.nodered.org/u/rei_vilo)\
**Post date:** [12 March 2022 10:30 UTC](https://discourse.nodered.org/t/is-this-a-real-json-string-im-not-able-to-extract-a-value/59747/2 "2022-03-12T10:30:23Z")

</div>

Hi!

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 March 2022 10:38 UTC](https://discourse.nodered.org/t/is-this-a-real-json-string-im-not-able-to-extract-a-value/59747/3 "2022-03-12T10:38:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [12 March 2022 11:59 UTC](https://discourse.nodered.org/t/is-this-a-real-json-string-im-not-able-to-extract-a-value/59747/4 "2022-03-12T11:59:03Z")

</div>

> [@lumanga](#):
>
> 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!

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 March 2022 12:22 UTC](https://discourse.nodered.org/t/is-this-a-real-json-string-im-not-able-to-extract-a-value/59747/5 "2022-03-12T12:22:08Z")

</div>

> [@lumanga](#):
>
> 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!

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.

---

<div class="post-metadata">

**Author:** ![lumanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lumanga/32/58334_2.png) [@lumanga](https://discourse.nodered.org/u/lumanga)\
**Post date:** [12 March 2022 14:11 UTC](https://discourse.nodered.org/t/is-this-a-real-json-string-im-not-able-to-extract-a-value/59747/6 "2022-03-12T14:11:25Z")

</div>

> [@Colin](#):
>
> msg.payload.IJFV

The Json is complaying.

 ![jsonHelp](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/2848d2dd2e08755930a7257343fb55ebf032edae.png)

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 March 2022 14:47 UTC](https://discourse.nodered.org/t/is-this-a-real-json-string-im-not-able-to-extract-a-value/59747/7 "2022-03-12T14:47:30Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [11 May 2022 14:48 UTC](https://discourse.nodered.org/t/is-this-a-real-json-string-im-not-able-to-extract-a-value/59747/8 "2022-05-11T14:48:19Z")

</div>

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