Ask Your Question
2

Is there a general way to convert ROS messages into JSON format?

asked 2014-10-30 21:30:04 -0600

liangfok gravatar image

updated 2014-10-30 21:30:27 -0600

I would like to convert arbitrary ROS messages into JSON format. Is there an official way of doing this?

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted
5

answered 2014-10-31 12:30:53 -0600

Wolf gravatar image

updated 2015-11-03 09:06:32 -0600

lucasw gravatar image

Rosbridge provides you a JSON API to ROS

http://wiki.ros.org/rosbridge_suite

edit flag offensive delete link more
2

answered 2017-10-18 16:17:29 -0600

vschmidt gravatar image

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
        }
    }
}
edit flag offensive delete link more
1

answered 2015-11-02 06:25:20 -0600

updated 2015-11-02 06:25:54 -0600

I just tried the rospy_message_converter.

Works really well! :)

edit flag offensive delete link more
0

answered 2014-10-31 12:15:06 -0600

Airuno2L gravatar image

I don't know much about this topic, but you might be able to find something useful in the newly announced mongodb_log package, I believe MongoDB uses JSON, and I think in that package they do automated conversions from ROS messages to JSON.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2014-10-30 21:30:04 -0600

Seen: 7,244 times

Last updated: Oct 18 '17