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

rospy_message_converter json2ros

asked 2020-10-19 10:47:17 -0500

Aviad gravatar image

Hi,

I'm trying to send a string msg after it will be converter by the library rospy_message_converter from JSON to ros. I need to send the message over the std_msgs/String type but with some sub-values in it. Like: under the data struct it will be:

        "name" : "Ben" 
         "age" : 15
         "weight": 50

I couldn't find any example of something like that. Again, I need to send this value by std_msgs/String and not with a custom msg.

This is what I tried:

from rospy_message_converter import json_message_converter
from std_msgs.msg import String
import rospy, json

if __name__ == "__main__":
rospy.init_node('api_test', anonymous=True)
name = 'Ben'
age = '15'

# json_str = '{"data": {}'.format('{"Engaged": {}}'.format(engaged))
json_str = '{"data": {"name": {0}}}'.format(name)

message = json_message_converter.convert_json_to_ros_message('std_msgs/String', json_str)
pub = rospy.Publisher("/pub",String, queue_size=1)
while not rospy.is_shutdown():
    pub.publish(message)
    rospy.sleep(0.5)

Thanks a lot, Aviad

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-12-18 12:52:15 -0500

kscottz gravatar image

Are you sure you have your python string formatted correctly? The python string bracket syntax is a bit tricky.

In [3]: name = "ASDFASDF"

In [4]: '{"data": {"name": {0}}}'.format(name)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-4-2aaad2f4d4cc> in <module>()
----> 1 '{"data": {"name": {0}}}'.format(name)

KeyError: '"data"'

In [5]: '{{"data": {{"name": {0}}}}}'.format(name)
Out[5]: '{"data": {"name": ASDFASDF}}'
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2020-10-19 10:47:17 -0500

Seen: 229 times

Last updated: Dec 18 '20