CBOR: Encode Node

The CBOR: Encode Node converts a JSON object on the payload into Concise Binary Object Representation format.

CBOR: Encode Node

Node Properties

The CBOR: Encode Node takes the following properties …

Input

  • JSON Input Path: (Required) A payload path pointing to the data on the payload you wish to encode.

Result

  • Output Encoding: The encoding method to apply to the data. Options are:

    • Base64
    • Binary
    • Hex (default)
  • CBOR Result Path: (Required) A payload path to where the encoded CBOR data should be placed on the payload.

Errors

  • Error Result Path: (Optional) A payload path for where to place info about any errors that occur while attempting to encode the value at JSON Input Path.

Read more about handling errors from this node below.

Node Example

Given the following payload …

{
  "data": {
    "hello": "world",
    "please": {
      "encode": "me",
      "as": ["C","B","O","R"],
      "times": 1
    }
  }
}

… and a CBOR: Encode Node with the following settings …

  • JSON Input Path: data
  • Output Encoding: Hex
  • CBOR Result Path: working.encoded

The payload after execution of the CBOR: Encode Node would look like:

{
  "data": {
    "hello": "world",
    "please": {
      "encode": "me",
      "as": ["C","B","O","R"],
      "times": 1
    }
  },
  "working": {
    "encoded": "a26568656c6c6f65776f726c6466706c65617365a366656e636f6465626d656261738461436142614f61526574696d657301"
  }
}

Use Case

The CBOR: Encode Node is commonly used in conjunction with the CBOR: Decode Node as a means of reducing bandwidth usage by an Edge Compute device. For example …

  1. In an Edge Workflow, a CBOR: Encode Node converts a JSON object into an encoded value, reducing the message’s size in bytes.
  2. That value is then published to the Losant MQTT broker on a custom topic using the MQTT Output Node.
  3. In an Application Workflow, an MQTT Trigger fires on receipt of the message.
  4. The CBOR: Decode Node converts the encoded payload back into its original JSON object.
  5. The rest of the workflow execution can save that data as device state or take some other action within the application.

Node Errors

In the case that the value found at the JSON Input Path fails to encode, no value will be placed at the CBOR Result Path; however, an object detailing the error will be placed at the Error Result Path if a path is provided.

Below is an example of an error stored on the payload at the path working.cborError:

{
  "working": {
    "cborError": {
      "type": "CBOR_ENCODE_NODE_ERROR",
      "message": "Encoding must be one of the following: base64, binary, hex."
    }
  }
}

Was this page helpful?


Still looking for help? You can also search the Losant Forums or submit your question there.