ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Rosbridge is probably the better answer, but just in case, here's a method that will at least work on messages whose content can be rendered as a string with the python str() function.

import json
import yaml

def msg2json(msg):
   ''' Convert a ROS message to JSON format'''
   y = yaml.load(str(msg))
   return json.dumps(y,indent=4)

if __name__ == "__main__":
    from geometry_msgs.msg import PoseStamped
    P = PoseStamped()
    print msg2json(P)

{
    "header": {
        "stamp": {
            "secs": 0, 
            "nsecs": 0
        }, 
        "frame_id": "", 
        "seq": 0
    }, 
    "pose": {
        "position": {
            "y": 0.0, 
            "x": 0.0, 
            "z": 0.0
        }, 
        "orientation": {
            "y": 0.0, 
            "x": 0.0, 
            "z": 0.0, 
            "w": 0.0
        }
    }
}