rospy_message_converter json2ros
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
Asked by Aviad on 2020-10-19 10:47:17 UTC
Answers
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}}'
Asked by kscottz on 2020-12-18 13:52:15 UTC
Comments