# Replace variable name from object?

**URL:** <https://discourse.nodered.org/t/replace-variable-name-from-object/20592>\
**Category:** General\
**Created:** [19 January 2020 19:37 UTC](https://discourse.nodered.org/t/replace-variable-name-from-object/20592 "2020-01-19T19:37:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![slurpey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/slurpey/32/16320_2.png) [@slurpey](https://discourse.nodered.org/u/slurpey)\
**Post date:** [19 January 2020 19:37 UTC](https://discourse.nodered.org/t/replace-variable-name-from-object/20592/1 "2020-01-19T19:37:06Z")

</div>

Hi,  
I'm receiving an IFTTT webhookrelay from Google Home object and I'm trying to use its content.  
It outputs this:

```auto
    {
        "type": "webhook",
        "meta": {
            "id": "815a9995-208e-44fd-be82-45ec2112dd6f",
            "bucked_id": "1ecec89d-b44e-4c53-87c5-f2abcc38392d",
            "bucket_name": "gactions",
            "input_id": "68922408-5099-4ab7-89b5-844210568a24",
            "input_name": "Default public endpoint",
            "output_name": "",
            "output_destination": ""
        },
        "headers": {
            "X-Newrelic-Id": [
                "VwAOU1RRGwAFUFZUAwQE"
            ],
            "X-Newrelic-Transaction": [
                "PxQGUgNUClYGB1lWVgFSUUYdUFIOFQZOElMLBw8KUQRQXQ0AAQQEQEgUUQMDW1kEVQZDPw=="
            ],
            "Content-Type": [
                "application/json"
            ],
            "Content-Length": [
                "58"
            ]
        },
        "query": "",
        "body": "{ \"time_google\": \"7.30\", \"message_google\": \"the potatoes are burning\" }",
        "method": "POST"
    }

```

Via a Function, then a JSON convert, I'm able to output

```auto

    Message.payload : object
        {"time_google":"7","message_google":"the potatoes are burning"}

```

Now I'm stuck. I need to change the payload (of "message\_google" above) to  
msg.payload.message = "the potatoes are burning"

(so I can cast this on another google device)

Help please? I've tried so many things....

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [22 January 2020 18:15 UTC](https://discourse.nodered.org/t/replace-variable-name-from-object/20592/2 "2020-01-22T18:15:00Z")

</div>

You have a nicely formatted JSON event object as input -- but the `msg.body` property is a JSON string?

In a function node, you can parse the body string into an object, and then add a new "message" property, using code like this (untested):

```auto
// parse the json body string
var msgBody = JSON.parse(msg.body);

// replace the entire payload with a new object...
msg.payload = { "message": msgBody.message_google };
// ... or add the message to the existing payload object
msg.payload.message = msgBody.message_google;

return msg;

```

For those who prefer NOT to actually code, use a `json` node to convert `msg.body` to an object.  
Then wire that to a `change` node and add a rules to:

> "Set:" `msg.payload.message` "To:" `msg.body.message_google`

Oh, and for the adventurous types who like compact "one-liners", skip the `json` node and use this JSONata expression to convert it to an object AND extract the new message to its own payload:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/04a3d10b00f99d4b7bddffdb81b7931361b74a68.png)

---

<div class="post-metadata">

**Author:** ![slurpey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/slurpey/32/16320_2.png) [@slurpey](https://discourse.nodered.org/u/slurpey)\
**Post date:** [23 January 2020 20:08 UTC](https://discourse.nodered.org/t/replace-variable-name-from-object/20592/3 "2020-01-23T20:08:08Z")

</div>

Thank you very much for this. I struggled a little bit but I ended up with a json convert and the following function:

```auto
var msg = {payload:{ontime: msg.payload.time_google, message: msg.payload.message_google}};
return msg;

```

which worked. Thanks a lot!
