MATLAB: Handle invalid fieldnames when parsing python dict to matlab via struct

fieldnamesMATLABpythonstruct

I am trying to convert a python dict to a Matlab struct like so:
pyClass = py.someClass.someSubClass();
data = x.someFunction();
data = struct(data);
This usually works fine, however, Matlab throws the following error:
Error using py.dict/struct
Invalid field name "yyy/xxx"
I know that Matlab requires the following three conditions for fieldnames to be valid:
start with a letter, otherwise assigning to that field will error
contain only letters, numbers, and/or the underscore character, otherwise assigning to that field will error,
and must be no longer than namelengthmax (currently 63) characters, otherwise you will receive a warning and the field name will be truncated
Does anyone know how I can handle this error – i.e. have a small python function that I can call from within Matlab to remove invalid characters?
Here is a snippet of what the python dict looks like (in this case the "/" is the issue):
data =
Python dict with no properties.
{'BTC/USD': {'fee_loaded': False, 'percentage': True, 'tierBased': True, 'maker': 0.001, 'taker': 0.002, 'tiers': {'taker': [[0, 0.002], ...
Thanks in advance!

Best Answer

I solved the issue by coverting the dict within python to json and then outputting the json to matlab where I then decode the data to retrieve the final struct which works perfectly fine for me so far. Here is the solution that I came up with:
pyClass = py.someClass.someSubClass();
data = x.someFunction();
data = py.json.dumps(data);
data = char(data);
data = jsondecode(data);