rclpy_message_converter.json_message_converter module

rclpy_message_converter.json_message_converter.convert_json_to_ros_message(message_type, json_message, strict_mode=True)

Takes in the message type and a JSON-formatted string and returns a ROS message.

Parameters:
  • message_type (str) – The desired ROS message type of the result

  • json_message (str) – A JSON-formatted string

  • strict_mode (bool, optional) – If strict_mode is set, an exception will be thrown if the json message contains extra fields.

Returns:

A ROS message

Return type:

class:genpy.Message

Example:
>>> msg_type = "std_msgs/msg/String"
>>> json_msg = '{"data": "Hello, Robot"}'
>>> convert_json_to_ros_message(msg_type, json_msg)
std_msgs.msg.String(data='Hello, Robot')
rclpy_message_converter.json_message_converter.convert_ros_message_to_json(message, base64_encoding=True)

Takes in a ROS message and returns a JSON-formatted string.

Parameters:
  • message (class:genpy.Message) – A ROS message to convert

  • base64_encoding (bool, optional) – If this parameter is true, encode all variable-size uint8[] or fixed-size uint8[n] fields using Base64 encoding. This saves a lot of space when converting large messages (such as sensor_msgs/Image) to json format. If this parameter is False, these fields will be converted to array.array (for variable-size fields) or numpy.ndarray (for fixed-size fields) instead.

Returns:

A JSON-formatted string

Return type:

str

Example:
>>> import std_msgs.msg
>>> ros_message = std_msgs.msg.String(data="Hello, Robot")
>>> convert_ros_message_to_json(ros_message)
'{"data": "Hello, Robot"}'