Node-RED 系列(4)-- Core nodes (Change/Switch/Json)

Change

Set, change, delete or move properties of a message, flow context or global context.
The node can specify multiple rules that will be applied in the order they are defined.

The available operations are:

set a property. The value can be a variety of different types or can be taken from an existing message or context property.


search & replace parts of the property. If regular expressions are enabled, the "replace with" property can include capture groups, for example $1.
Replace will only change the type if there is a complete match.


delete a property


move or rename a property

The "expression" type uses the JSONata query and expression language.

Usage examples:

Switch

The Switch node allows messages to be routed to different branches of a flow by evaluating a set of rules against each message.

When a message arrives, the node will evaluate each of the defined rules and forward the message to the corresponding outputs of any matching rules.
Optionally, the node can be set to stop evaluating rules once it finds that matches.
The rules can be evaluated against an individual message property, a flow or global context property, environment variable or the result of a JSONata expressions.

There are four types of rule:

  1. Value rules are evaluated against the configured property.
  2. Sequence rules can be used on message sequences, such as those generated by split node.
  3. A JSONata expression can be provided that will be evaluated against the whole message and will match if the expression returns a true value.
  4. An otherwise rule can be used to match if one of the preceding rules have matched.


The is true/false and is null rules perform strict comparisons against those types. They do not convert between types.

The is empty and is not empty rules can be used to test the length of String, Arrays and Buffers, or the number of properties an Object has. Neither rule will pass if the property being tested has a boolean, null or undefined value.

By default, the node does not modify the msg.parts property of messages that are part of a sequence.

The recreated message sequences option can be enabled to generate new message sequences for each rule that matches. In this mode, the node will buffer the entire the incoming sequence before sending the new sequences on. The runtime setting nodeMessageBufferMaxLength can be used to limit how many message nodes will buffer.

Usage examples:

Json

Converts between a JSON string and its JavaScript object representation, in either direction.

Inputs

A JavaScript object or JSON string.


An optional JSON Schema object to validate the payload against. The property will be deleted before the msg is sent to the next node.

Outputs

  • If the input is a JSON string it tries to parse it to a JavaScript object.
  • If the input is a JavaScript object it creates a JSON string. The string can optionally be well-formatted.


If JSON schema validation fails, the catch node will have a schemaError property containing an array of errors.

By default, the node operates on msg.payload, but can be configured to any message property.

The node can also be configured to ensure a particular encoding instead of toggling between the two. This can be used, for example, with the Http In node to ensure the payload is a parsed object even if an incoming request did not set its content-type correctly for the HTTP In node to do the conversion.

If the node is configured to ensure the property is encoded as a String and it receives a String, no further checks will be made of the property. It will not check the String is valid JSON nor will it reformat it if the format option is selected.

Usage examples:

你可能感兴趣的:(Node-RED 系列(4)-- Core nodes (Change/Switch/Json))