# Save variable to object type

**URL:** <https://discourse.nodered.org/t/save-variable-to-object-type/12254>\
**Category:** General\
**Created:** [15 June 2019 06:27 UTC](https://discourse.nodered.org/t/save-variable-to-object-type/12254 "2019-06-15T06:27:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![limengsong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/limengsong/32/9391_2.png) [@limengsong](https://discourse.nodered.org/u/limengsong)\
**Post date:** [15 June 2019 06:27 UTC](https://discourse.nodered.org/t/save-variable-to-object-type/12254/1 "2019-06-15T06:27:13Z")

</div>

In node-red, how to save a variable to an object structure, not a string. Such as a JSON object

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [15 June 2019 06:30 UTC](https://discourse.nodered.org/t/save-variable-to-object-type/12254/2 "2019-06-15T06:30:14Z")

</div>

Hi @limengsong

Can you give an example of what you mean?

---

<div class="post-metadata">

**Author:** ![limengsong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/limengsong/32/9391_2.png) [@limengsong](https://discourse.nodered.org/u/limengsong)\
**Post date:** [15 June 2019 06:43 UTC](https://discourse.nodered.org/t/save-variable-to-object-type/12254/3 "2019-06-15T06:43:10Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/560c58e6a7c0415651beba14b18bd6f5b7dca3f1.png)  
as the picture shows:  
There are two properties for the workCenterCode and workCenterName name.  
I want to use an attribute instead, such as workcenter:{code:"xxx",name:"xxx"}  
Save as an object instead of a string.  
The front-end component selects the select component.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [15 June 2019 06:50 UTC](https://discourse.nodered.org/t/save-variable-to-object-type/12254/4 "2019-06-15T06:50:14Z")

</div>

How are you generating that object today?

There are lots of ways to structure a message object however you want it. The Function node with some JavaScript, the Change node can do it.

I'm not going to list out all the different ways it can be done.

If you can show us how you are building that object today, we can show you how to modify it to get what you want.

---

<div class="post-metadata">

**Author:** ![limengsong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/limengsong/32/9391_2.png) [@limengsong](https://discourse.nodered.org/u/limengsong)\
**Post date:** [15 June 2019 06:54 UTC](https://discourse.nodered.org/t/save-variable-to-object-type/12254/5 "2019-06-15T06:54:50Z")

</div>

```auto
    <div class="form-row">
        <label for="node-input-workcenterCode" data-i18n="workcenter.name" ><i class="icon-tag"></i> </label>
        <select type="text" id="node-input-workcenterCode" style="width:70%;">
        </select>
    </div>

        oneditprepare: function () {
            var node = this;
            $.ajax({
                url: `/api/workCenters`,
                dataType: 'json',
                type: 'GET',
                success: (data) => {
                    if (data.data && data.data.length > 0) {
                        $('#node-input-workcenterName').val(`${data.data[0].code}`)
                        for (let v of data.data) {
                            if (v.code !== node.workcenterCode) {
                                $("#node-input-workcenterCode").append(
                                    `<option value=${v.code}>${v.name}</option>`);
                            } else {
                                $("#node-input-workcenterCode").append(
                                    `<option value=${v.code} selected = "selected" >${v.name}</option>`
                                );
                            }
                          
                        }
                    }
                },
                error: (data) => {
                    //error
                }
            });

        },

```

Thanks!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [15 June 2019 06:58 UTC](https://discourse.nodered.org/t/save-variable-to-object-type/12254/6 "2019-06-15T06:58:23Z")

</div>

Oh, you are talking about node properties. I thought you were asking about message structure.

You can use oneditsave you set node properties to whatever structure you want.

The Change node, for example, does just that for the list of rules you set in the node. Have a look at its html.

---

<div class="post-metadata">

**Author:** ![limengsong](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/limengsong/32/9391_2.png) [@limengsong](https://discourse.nodered.org/u/limengsong)\
**Post date:** [15 June 2019 07:09 UTC](https://discourse.nodered.org/t/save-variable-to-object-type/12254/7 "2019-06-15T07:09:49Z")

</div>

@knolleary

```auto
        oneditsave: function () {
            this.workcenter=[{code:"111",name:"222"}];
        },

```

like this?

Thanks
