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

Reverting data type from string/JSON

asked 2022-04-05 11:45:19 -0600

Flash gravatar image

updated 2022-04-06 13:07:10 -0600

Hi,
I had to convert msg like geometry_msg data type to string so that I can upload it to the MYSQL database. I was wondering if there is a way that can revert String back to the original data type. Instead of me trying to use split and other stuff.

[MODIFIED]
After trying to convert to string and store it in the MYSQL database it was generating issues to revert it back, so instead of converting it to string, I had to use JSON format to save it to the database.

creating the table in with json format data type CREATE TABLE IF NOT EXISTS room_data (id INT AUTO_INCREMENT PRIMARY KEY, room_name VARCHAR(255), data JSON)"

Thanks in advance,

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-04-06 02:01:13 -0600

ijnek gravatar image

updated 2022-04-06 02:01:57 -0600

If you've converted the msg to string using a yaml format, you could read it back into a message type by first loading it into a dictionary using yaml.safe_load, and then filling a message object using rosidl_runtime_py.set_message_fields.

Here's an example of a geometry_msgs.msg.Pose being converted to string, and then restored back into a new object:

#!/usr/bin/env python3

import yaml
from geometry_msgs.msg import Pose
from rosidl_runtime_py import message_to_yaml, set_message_fields

# Create Pose msg
original = Pose()
original.position.x = 1.0
original.position.y = 2.0
original.position.z = 3.0

# Convert to string
pose_string = message_to_yaml(original)
print("Pose is: \n", pose_string)

# Convert back to yaml
values_dictionary = yaml.safe_load(pose_string)
restored = Pose()
set_message_fields(restored, values_dictionary)

print("Restored x, y, z: ", restored.position.x, restored.position.y, restored.position.z)
edit flag offensive delete link more

Comments

@ijnek tried the method you mentioned. Restoring values doesn't work when data is stored in Pose_string format. So I used JSON datatype on the database. It needs a little more modifications to accommodate that.

after converting it to YAML using message_to_yaml

data_string = yaml.safe_load(data)
data_string = json.dumps(data_string, indent=4))

And it needs to import json and to convert back to ROS message is same as you mentioned. Can you please modify your answer with these lines so that I can mark it as correct?

Flash gravatar image Flash  ( 2022-04-06 10:39:30 -0600 )edit

I'm a bit confused with your code suggestion. if you're using a json string instead of a yaml string, you should just have to replace yaml.safe_load with json.loads:

import json

# Convert back to yaml
values_dictionary = json.loads(pose_string)
restored = Pose()
set_message_fields(restored, values_dictionary)
print("Restored x, y, z: ", restored.position.x, restored.position.y, restored.position.z)

Any additional settings such as indent would be up to the use case.

ijnek gravatar image ijnek  ( 2022-04-06 17:53:55 -0600 )edit

Converting back to YAML works both ways using json.loads or by yaml.safe_load but saving data to the database does not work with only message_to_yaml have to use safe_load and json.dumps

# Storing data on database
data = message_to_yaml(data)
data_string = yaml.safe_load(data)
db.create_room(room_name, json.dumps(data_string, indent=4))

So your solution works when it just has to convert to string, but not helping with loading on MySQL database. I had followed this link for converting it to JSON.
Just in case if you want to know the error I am getting when I store using your method mysql.connector.errors.DataError: 3140 (22032): Invalid JSON text: "Invalid value." at position 0 in value for column 'cleaned_room.data'.

Flash gravatar image Flash  ( 2022-04-07 10:31:37 -0600 )edit

Got it, thanks for the clarification.

ijnek gravatar image ijnek  ( 2022-04-07 18:41:44 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2022-04-05 11:45:19 -0600

Seen: 467 times

Last updated: Apr 06 '22