Accessing variables in JSON

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.

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...

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...

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

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.

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.

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?

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.

As I said...


OK, glad it is sorted.

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.

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