# Accessing variables in JSON

**URL:** <https://discourse.nodered.org/t/accessing-variables-in-json/46206>\
**Category:** Developing Nodes\
**Created:** [25 May 2021 10:49 UTC](https://discourse.nodered.org/t/accessing-variables-in-json/46206 "2021-05-25T10:49:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nacartwright](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nacartwright/32/41906_2.png) [@nacartwright](https://discourse.nodered.org/u/nacartwright)\
**Post date:** [25 May 2021 10:49 UTC](https://discourse.nodered.org/t/accessing-variables-in-json/46206/1 "2021-05-25T10:49:02Z")

</div>

I have a node that I need to build a JSON string but need the payload to be dynamic. The data that I need to use to populate the variables are deep inside another JSON string.

I've tried saving my variable data in context for both the node and flow context. I've attempted using a JSON typedInput and JSONata. The JSON input does not like any variables while the JSONata gripes at the $flowContext function.

This is on a custom node too. Need to have an input to build what I need to be in the outgoing message.

---

<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 May 2021 11:28 UTC](https://discourse.nodered.org/t/accessing-variables-in-json/46206/2 "2021-05-25T11:28:35Z")

</div>

> [@nacartwright](#):
>
> This is on a custom node too. Need to have an input to build what I need to be in the outgoing message.

First, an understanding of the relationship between JSON and a JavaScript object is necessary.

- JSON is a string representation of a JavaScript `object`
- The JSON node or `JSON.parse(json_str);` can convert a JSON `string` to a JavaScript `object`
- The JSON node or `JSON.stringify(js_obj);` can convert a JavaScript `object` to a JSON `string`

Based on that, you can easily set a variable value in a JavaScript `object` then convert it to JSON & vice versa.

example 1...

```auto
var myObject = {
  static1: "static value",
  dynamic1: some_var, // << some_var could be a number or string or another object
}
var myJSON = JSON.stringify(myObject);

```

example 2...

```auto
var myObject = JSON.parse(some_json) 
myObject.dynamic1 = some_var; //update dynamic1 with new value
var new_JSON = JSON.stringify(myObject); //convert back to JSON

```

NOTE: Why must the input be JSON? Remember, JSON is a string.  
If you force the user of your contrib node to send JSON (string) they will have to convert every object into a string before sending it into your node.

A typedInput can allow object or string (JSON) and automatically parses the users input.

* * *

PS: It is not 100% clear if you are creating a custom (contrib) node or building a flow  
You are talking about JSONata/$flowContext - but these are flow things not custom node things

---

<div class="post-metadata">

**Author:** ![nacartwright](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nacartwright/32/41906_2.png) [@nacartwright](https://discourse.nodered.org/u/nacartwright)\
**Post date:** [25 May 2021 12:39 UTC](https://discourse.nodered.org/t/accessing-variables-in-json/46206/3 "2021-05-25T12:39:49Z")

</div>

I need to be able to pass in various parameters depending on the flow being built in the DialogFlow request. In this scenario I'm trigging an event in DialogFlow but I need to pass parameters as a JSON object.

Ideally, there is a way to specify parameter names + values in the edit input field. But the values are going to be in the flow context.

 ![Screenshot 2021-05-25 083427](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/1129d359f2cfb90d322f598c619f99907d1ea3d6.png)

---

<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 May 2021 12:44 UTC](https://discourse.nodered.org/t/accessing-variables-in-json/46206/4 "2021-05-25T12:44:54Z")

</div>

> [@nacartwright](#):
>
> I need to pass parameters as a JSON object

There is no such thing as a "JSON object". JSON is a string representation of a JavaScript object. It's one or the other not both. Sorry to sound like I'm nit picking but it just doesn't make sense.

> [@nacartwright](#):
>
> But the values are going to be in the flow context

What do you mean by this? Is the custom contrib node you are developing going to access flow context?

Ps. Please clarify you are creating a custom (contrib) node that you will eventually publish to the flow library/NPM - yes?

---

<div class="post-metadata">

**Author:** ![nacartwright](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nacartwright/32/41906_2.png) [@nacartwright](https://discourse.nodered.org/u/nacartwright)\
**Post date:** [25 May 2021 12:56 UTC](https://discourse.nodered.org/t/accessing-variables-in-json/46206/5 "2021-05-25T12:56:13Z")

</div>

Yes, custom contrib node that needs to build a JSON object to add to the response. I think I figured it out. When I did a JSONata expression editor on my node and seems to be working now.

---

<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 May 2021 12:57 UTC](https://discourse.nodered.org/t/accessing-variables-in-json/46206/6 "2021-05-25T12:57:30Z")

</div>

> [@nacartwright](#):
>
> JSON object

As I said...

> [@Steve-Mcl](#):
>
> There is no such thing as a "JSON object". JSON is a string representation of a JavaScript object. It's one or the other not both. Sorry to sound like I'm nit picking but it just doesn't make sense.

  

> [@nacartwright](#):
>
> When I did a JSONata expression editor on my node and seems to be working now

OK, glad it is sorted.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [25 May 2021 13:06 UTC](https://discourse.nodered.org/t/accessing-variables-in-json/46206/7 "2021-05-25T13:06:59Z")

</div>

> [@Steve-Mcl](#):
>
> custom contrib node you are developing going to access flow context?

Custom nodes should not access flow or global context - as a) they will pollute it for others b) may get accessed/overwritten by others c) may leak internal information that they shouldn't. Either they have their own private context, or they pass out data for others to use.

---

<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:** [24 July 2021 13:07 UTC](https://discourse.nodered.org/t/accessing-variables-in-json/46206/8 "2021-07-24T13:07:29Z")

</div>

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