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

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

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

liangfok gravatar image

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

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
7

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

Wolf gravatar image

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

lucasw gravatar image

Rosbridge provides you a JSON API to ROS

http://wiki.ros.org/rosbridge_suite

edit flag offensive delete link more

Comments

Is there a way to just run rosbridge_library (to just get ROS messages to JSON conversion)?

parzival gravatar image parzival  ( 2020-06-24 11:07:56 -0500 )edit
2

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

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

Comments

Hi,
The above solution was helpful thanks for that. I have another question about how do you revert it back to ros msg.

Flash gravatar image Flash  ( 2022-04-06 08:32:05 -0500 )edit
4

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

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

I just tried the rospy_message_converter.

Works really well! :)

edit flag offensive delete link more
1

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

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

Question Tools

5 followers

Stats

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

Seen: 17,629 times

Last updated: Oct 18 '17