# Referring to Json object problem

**URL:** <https://discourse.nodered.org/t/referring-to-json-object-problem/76276>\
**Category:** General\
**Created:** [8 March 2023 07:48 UTC](https://discourse.nodered.org/t/referring-to-json-object-problem/76276 "2023-03-08T07:48:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vilnis\_R](https://avatars.discourse-cdn.com/v4/letter/v/a183cd/32.png) [@Vilnis\_R](https://discourse.nodered.org/u/Vilnis_R)\
**Post date:** [8 March 2023 07:48 UTC](https://discourse.nodered.org/t/referring-to-json-object-problem/76276/1 "2023-03-08T07:48:00Z")

</div>

Hi,  
I have problem with javascript referring to particular keys. I need to rearrange incoming Json to other Json layout however debug throws me an error: "TypeError: Cannot read property 'RP' of undefined". My guess that it is because key names are stored as strings in the array dataNames. I think that they should be called in some other way but I can not figure out how. Here is the function code & incoming Json:

```auto
[
    {
    "0":{"RP":55.9,"AP":87.3,"PF":0.6,"V":230.8,"I":0.4},
    "1":{"RP":391.3,"AP":467.9,"PF":0.8,"V":227.6,"I":2.1},
    "2":{"RP":47.7,"AP":80.9,"PF":0.6,"V":231.4,"I":0.3},
    "3":{"T":22.1,"H":23.1,"P":987.2,"A":219}
    }
]

function myFunction(data)
{
    let arrange = [];
    const colNames = ['Real Power','Apparent Power','Power Factor','Voltage','Amps'];
    const dataNames = ['RP','AP','PF','V','I'];

    for (i=0; i<5; i++)
    {
        let newData = {
            "" : colNames[i],
            "Phase 1": data['0'][dataNames[i]],
            "Phase 2": data['1'][dataNames[i]],
            "Phase 3": data['2'][dataNames[i]]
        }

        arrange.push(newData);
    }

    return arrange;
}

var msgout = myFunction(msg.payload);
msg.payload = msgout;
return msg;

```

---

<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:** [8 March 2023 08:02 UTC](https://discourse.nodered.org/t/referring-to-json-object-problem/76276/2 "2023-03-08T08:02:38Z")

</div>

Your object is in an array of 1 element, try sending data as `myFunction(msg.payload[0]);`

---

<div class="post-metadata">

**Author:** ![Vilnis\_R](https://avatars.discourse-cdn.com/v4/letter/v/a183cd/32.png) [@Vilnis\_R](https://discourse.nodered.org/u/Vilnis_R)\
**Post date:** [8 March 2023 08:07 UTC](https://discourse.nodered.org/t/referring-to-json-object-problem/76276/3 "2023-03-08T08:07:00Z")

</div>

What an epic fail! 🙂 Yes, of course you are right and it works. Well I spent an hours to dig into enums and some other stuff while solution was much more silly. Thanks a lot! 🙂

---

<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:** [8 March 2023 12:24 UTC](https://discourse.nodered.org/t/referring-to-json-object-problem/76276/4 "2023-03-08T12:24:17Z")

</div>

Don't forget that if you use the debug node, you can grab the correct path from the debug display.

---

<div class="post-metadata">

**Author:** ![Vilnis\_R](https://avatars.discourse-cdn.com/v4/letter/v/a183cd/32.png) [@Vilnis\_R](https://discourse.nodered.org/u/Vilnis_R)\
**Post date:** [9 March 2023 15:14 UTC](https://discourse.nodered.org/t/referring-to-json-object-problem/76276/5 "2023-03-09T15:14:32Z")

</div>

Thanks a lot. Yes, I actually use this copy path option in debug but occasionally not this time. That is why I got in trouble. 🙂

---

<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:** [23 March 2023 15:14 UTC](https://discourse.nodered.org/t/referring-to-json-object-problem/76276/6 "2023-03-23T15:14:49Z")

</div>

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