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

ROS can't read json file

asked 2022-02-17 09:09:57 -0500

ENTMar gravatar image

updated 2022-02-17 16:50:28 -0500


I am trying to communicate a non ros-program to ros kinetic via MQTT Broker. mqtt_bridge node make it easier and I already configurated it properly. When I launch roslaunch mqtt_bridge demo.launch it runs very well without any problem. As soon I start the communication from a non ros-programm to ros, the launch file throw this error message multiple times "Message type std_msgs/String does not have a field position".

The message program looks like this:

import json
import paho.mqtt.client as mqtt
import time

client = mqtt.Client()
client.connect(, 1883, 60)
msg = {
    'position': [
            'x_value' : '5',
            'y_value' : '2.6'
 channel = "ZIRP/FTS/N1/Pos"
 client = mqttClient
 client.publish(channel, json_msg)

The code is written in python and run in a non ros PC. The code doesn't throw any problem and the mqtt broker receive the message successfully. The sent data structure is string and probably in array but I'm not sure.

The configuration in mqtt_bridge node:

    protocol: 4      # MQTTv311
    port: 1883
    keepalive: 60
  private_path: device/001

   # echo back
   - factory: mqtt_bridge.bridge:RosToMqttBridge
      msg_type: std_msgs.msg:String
      topic_from: /echo
      topic_to: ZIRP/FTS/N1/Pos
   - factory: mqtt_bridge.bridge:MqttToRosBridge
     msg_type: std_msgs.msg:String
     topic_from: ZIRP/FTS/N1/Pos
     topic_to: /echo

Any idea why ros can't read the message?

edit retag flag offensive close merge delete


Please edit your question to provide more context. To be able to help you best, we need to be able to reproduce your problem. It looks like you're trying to send structured data on a topic that is expecting a string.

tfoote gravatar image tfoote  ( 2022-02-17 11:04:34 -0500 )edit

I edited my question and it's in the actual version.

ENTMar gravatar image ENTMar  ( 2022-02-17 11:22:23 -0500 )edit

I already configurated it properly

Please provide your configuration file.

tryan gravatar image tryan  ( 2022-02-17 11:56:33 -0500 )edit

The configuration file is included in the question right now

ENTMar gravatar image ENTMar  ( 2022-02-17 16:51:59 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2022-02-25 09:48:17 -0500

ENTMar gravatar image

Hello Community

thank you to ljaniec's support. I found an easiest way to transport my data from non ros program to ros via mqtt without using rospy_message_converter. The message type in rospy_message_converter is very sensitive and hard to convert a complex json file. In another way, the file can be transferred with mqtt function:

  1. In MQTT Publisher

    Create the dictionary (python) and convert to json, then send to the mqtt broker

  2. In MQTT Subscriber

    Convert back the json to dictionary. Instead of using the message library from ros (e.g std_msgs, geometry_msgs) you create your own message file like the json structure you sent. Last initialize the ros message equal the dictionary value and publish it to a topic.

edit flag offensive delete link more

answered 2022-02-17 18:38:10 -0500

ljaniec gravatar image

The String msg documentation shows that std_msgs/String has only data field.

"Message type std_msgs/String does not have a field position"

suggests that your program doesn't construct the ROS message like it should (with string in the data field of message).

When I did something similar, I have used this converter:

between JSON and ROS messages. Maybe you could use it to form everything correctly with similar steps - between MQTT and ROS: get MQTT message as string, loads it into JSON/Python dictionary, convert it to ROS message, publish. The only problem would be the ROS1 version used (I used it with Noetic).

edit flag offensive delete link more


Thank you for your suggestion. Unfortunately, the message program doesn't run ros. I would like to communicate a non-ros program to ros node via MQTT. I am thinking to use the converter between MQTT and ROS. Can that converter do that job? The process supposes to work in ROS PC. MQTT receive the JSON, convert it with that node and another node just subscribe to that message.

ENTMar gravatar image ENTMar  ( 2022-02-18 04:53:06 -0500 )edit

I think you could just use paho-mqtt client in your ROS node, where your MQTT client will publish in it's callback the converted ROS message further in the system through topics etc.

ljaniec gravatar image ljaniec  ( 2022-02-18 05:40:24 -0500 )edit

Thank you for your suggestion and I'm working on it. Can you help me with one more thing? I am sending this file in json: msg ={ "x_value": x, "y_value": y, "phi", phi}

subscriber from MQTT message = json_message_converter.convert_json_to_ros_message(message_type, msg)

when the data structure is unchangeable and given out in ros as the same sent message. which message type should I declare in the command below?

ENTMar gravatar image ENTMar  ( 2022-02-19 08:27:08 -0500 )edit

I would prepare your own ROS message there (but ONLY IF there is no other ROS message which could give you the same info). For your case, the geometry_msgs/Pose is a good candidate - the part with Point will have x and y, the Quaternion will have the angle phi (after transformation to the quaternion, using e.g. correct function from tf_transformations). If this is acceptable solution, please upvote and accept the answer :)

ljaniec gravatar image ljaniec  ( 2022-02-21 04:00:00 -0500 )edit

Question Tools



Asked: 2022-02-17 09:09:57 -0500

Seen: 495 times

Last updated: Feb 25 '22