# JSONata question

**URL:** <https://discourse.nodered.org/t/jsonata-question/27201>\
**Category:** General\
**Created:** [24 May 2020 16:31 UTC](https://discourse.nodered.org/t/jsonata-question/27201 "2020-05-24T16:31:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [24 May 2020 16:31 UTC](https://discourse.nodered.org/t/jsonata-question/27201/1 "2020-05-24T16:31:46Z")

</div>

I am extracting data from my smart meter and thought I could make this change node into a one liner. But for the life of me I cannot replace "calc" in the second entry in the node with the reference to the value in the first line. The value is a string containing a hex value. If it's not possible I am also very happy 🙂 ![Screenshot-1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/9175f20fccaf3ad6b312b2308597d334618fa073.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [24 May 2020 16:35 UTC](https://discourse.nodered.org/t/jsonata-question/27201/2 "2020-05-24T16:35:06Z")

</div>

What does the input payload look like ?

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [24 May 2020 16:43 UTC](https://discourse.nodered.org/t/jsonata-question/27201/3 "2020-05-24T16:43:53Z")

</div>

"00000B96" - for example, it's the actual consumption in Watt hours.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [24 May 2020 16:46 UTC](https://discourse.nodered.org/t/jsonata-question/27201/4 "2020-05-24T16:46:33Z")

</div>

Could you please post a debug output including the object ? (meaning the input that goes into this change node)

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [24 May 2020 16:53 UTC](https://discourse.nodered.org/t/jsonata-question/27201/5 "2020-05-24T16:53:49Z")

</div>

![Screenshot-2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/6/c6c011bad49ee1676d289542273e56b3a0bbc586.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [24 May 2020 17:04 UTC](https://discourse.nodered.org/t/jsonata-question/27201/6 "2020-05-24T17:04:35Z")

</div>

In jsonata i don't see a quick way as it does not seem to like concatenation and parsing it after that.

In a function node it works;

```auto
m = "0x"+msg.payload
return {WattsNow:parseInt(m)};

```

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [24 May 2020 17:09 UTC](https://discourse.nodered.org/t/jsonata-question/27201/7 "2020-05-24T17:09:58Z")

</div>

Oh yes, I have already done it in a function node with:

```auto
msg.Wh_now = parseInt(msg.payload.elecMtr["0702"]["04"]["00"], 16);

```

I was just wondering if it was possible in a change node with a simple JSONata expression.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [24 May 2020 17:10 UTC](https://discourse.nodered.org/t/jsonata-question/27201/8 "2020-05-24T17:10:58Z")

</div>

Fiddling a bit further, this works in jsonata:

```auto
$parseInteger($string("0x"&payload),'0')

```

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [24 May 2020 17:18 UTC](https://discourse.nodered.org/t/jsonata-question/27201/9 "2020-05-24T17:18:13Z")

</div>

Nice!, I'll try that later!

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [25 May 2020 15:21 UTC](https://discourse.nodered.org/t/jsonata-question/27201/10 "2020-05-25T15:21:25Z")

</div>

> [@bakman2](#):
>
> Fiddling a bit further, this works in jsonata:
> 
> ```auto
> $parseInteger($string("0x"&payload),'0')
> 
> ```

Yes that works, but the normal notation for referencing the value doesn't work in JSONata. You need to replace the square brackets with`"."` Took me a while, this works fine:

```auto
$parseInteger("0x"&payload.elecMtr."0702"."04"."00",'0')

```

---

<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:** [24 July 2020 15:21 UTC](https://discourse.nodered.org/t/jsonata-question/27201/11 "2020-07-24T15:21:27Z")

</div>

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