# Need help with creating an js object

**URL:** <https://discourse.nodered.org/t/need-help-with-creating-an-js-object/60339>\
**Category:** General\
**Tags:** function-node\
**Created:** [25 March 2022 12:36 UTC](https://discourse.nodered.org/t/need-help-with-creating-an-js-object/60339 "2022-03-25T12:36:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [25 March 2022 12:36 UTC](https://discourse.nodered.org/t/need-help-with-creating-an-js-object/60339/1 "2022-03-25T12:36:01Z")

</div>

My case:  
I want to create an object which takes the value of x as value of the key location.

the following code does not work ☹

```auto
context.set('location','UGSK');
let x = context.get('location').valueOf();

msg.payload = JSON.parse('{"location":x,"switch":"Regler NoOp"}');
return msg;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 March 2022 12:38 UTC](https://discourse.nodered.org/t/need-help-with-creating-an-js-object/60339/2 "2022-03-25T12:38:24Z")

</div>

There is no need to use JSON parse - just build the object!

```auto
let x = context.get('location').valueOf();
msg.payload = { 
 "location": x,
 "switch": "Regler NoOp"
};
return msg;

```

PS: not sure why you are calling `.valueOf();` ???

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [25 March 2022 12:55 UTC](https://discourse.nodered.org/t/need-help-with-creating-an-js-object/60339/3 "2022-03-25T12:55:23Z")

</div>

ok, the object should look like this:

```auto
        node_send(JSON.parse('{"location":"UGSK","switch":"Regler on"}'),context.get('out-regler'));

```

"UGSK" is the value of the context variable location

```auto
        const l = (context.get('location'));
        node_send(l,context.get('out_debug'));
        node_send(JSON.parse('{"location":context.get('location').value.Of,"switch":"Regler NoOp"}'),context.get('out_regler'));

```

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [25 March 2022 13:02 UTC](https://discourse.nodered.org/t/need-help-with-creating-an-js-object/60339/4 "2022-03-25T13:02:15Z")

</div>

please check the threat. did not reply to you. sorry

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [25 March 2022 13:07 UTC](https://discourse.nodered.org/t/need-help-with-creating-an-js-object/60339/5 "2022-03-25T13:07:21Z")

</div>

problem solved!  
I did not need JSON.parse()

---

<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:** [8 April 2022 13:07 UTC](https://discourse.nodered.org/t/need-help-with-creating-an-js-object/60339/6 "2022-04-08T13:07:46Z")

</div>

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