# Problem with json in custom node

**URL:** <https://discourse.nodered.org/t/problem-with-json-in-custom-node/88621>\
**Category:** Developing Nodes\
**Created:** [11 June 2024 13:17 UTC](https://discourse.nodered.org/t/problem-with-json-in-custom-node/88621 "2024-06-11T13:17:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![riri](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@riri](https://discourse.nodered.org/u/riri)\
**Post date:** [11 June 2024 13:17 UTC](https://discourse.nodered.org/t/problem-with-json-in-custom-node/88621/1 "2024-06-11T13:17:11Z")

</div>

Hi everyone

```auto
<script type="text/javascript">
    RED.nodes.registerType('InitSVI',{
        category: 'matrice_svi',
        color: '#EF4040',
        defaults: {
            name: {value:"",required:true},
            statName:{value:""},
            globalProperties:{value:""} //"", [] , {} // Object , JSON 
        },
        inputs: 0,
        outputs: 1,
        icon: "icons/InitSVI.svg",
        label: function() {
            return this.name||"InitSVI";
        },
        oneditprepare: function () {
            this.editor = RED.editor.createEditor({
                id: 'node-input-editor',
                mode: 'ace/mode/json',
                value: this.globalProperties
            });
        },
        oneditsave: function() {
            this.globalProperties = this.editor.getValue();
            this.editor.destroy();
            delete this.editor;
        },
        oneditcancel: function() {
            this.editor.destroy();
            delete this.editor;
        },
    });
</script>

<script type="text/html" data-template-name="InitSVI">

    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i>Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div class="form-row">
        <label for="node-input-statName"><i class="fa fa-tag"></i>statName</label>
        <input type="text" id="node-input-statName" placeholder="statName">
    </div>
    <label for="node-input-editor"><i class="fa fa-tag"></i>globalProperties</label>
    <div style="height: 90%; min-height:150px;width: 90%;margin: auto;" class="node-text-editor" id="node-input-editor"></div>

</script>

```

hello everyone,

i created a custom node that has a text editor to put json inside, except that when i import this node it converts the value to string, except that i would like it to keep the type JSON, i tried to change the type of the field "value" but without success. If anyone has an idea, I'm a taker

Riri

---

<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:** [11 June 2024 13:20 UTC](https://discourse.nodered.org/t/problem-with-json-in-custom-node/88621/2 "2024-06-11T13:20:10Z")

</div>

The text editor only knows how to edit strings - so the `getValue()` is giving you a string.

If you really want to store the parsed version instead, you could do:

```auto
this.globalProperties = JSON.parse(this.editor.getValue())

```

(and use `JSON.stringify(...)` in `oneditprepare` to convert it back to a string for the text editor to use.

_However_ you will need to handle the case where the user enters invalid JSON etc as that will cause the `JSON.parse` to throw an error.

---

<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:** [11 June 2024 14:28 UTC](https://discourse.nodered.org/t/problem-with-json-in-custom-node/88621/3 "2024-06-11T14:28:51Z")

</div>

Just adding a reminder to that suggestion - always wrap JSON.parse and JSON.stringify in `try`/`catch` blocks because they can (and do) fail fairly easily.

---

<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:** [11 June 2024 16:22 UTC](https://discourse.nodered.org/t/problem-with-json-in-custom-node/88621/4 "2024-06-11T16:22:09Z")

</div>

> [@riri](#):
>
> it converts the value to string, except that i would like it to keep the type JSON

For the record, JSON is always a string. When you parse it then it becomes a javascript object. It is no longer JSON.

---

<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:** [25 June 2024 16:22 UTC](https://discourse.nodered.org/t/problem-with-json-in-custom-node/88621/5 "2024-06-25T16:22:56Z")

</div>

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