I’d like to convert a string into dict. The input string is taken from the database.
Below is the log.before (this is the given string from the databse)given.
{'id': '25c16650-070c-4e17-8996-f8d79d1747d7', 'tariff': OrderedDict([('id', 1), ('title', 'dTtvdvemDLYDHZiPNfaDwgAsTopnQBvYiHVPJvlREQaqLtlJFkQVFKZhpmWWphaxedqzWZBcpXxglKZvgpzPzPVEkSnJfEHnVfHy'), ('price', '0.00'), ('label', '(None) dTtvdvemDLYDHZiPNfaDwgAsTopnQBvYiHVPJvlREQaqLtlJFkQVFKZhpmWWphaxedqzWZBcpXxglKZvgpzPzPVEkSnJfEHnVfHy')]), 'title': 'Test Product', 'price': None, 'establishment': 0.0, 'billing_period': 'M', 'additional_points': [], 'speed_unit': 'Gb', 'service_type': 'P', 'type': 'S', 'show_with_vat': True, 'price_with_vat': 0, 'establishment_with_vat': 0, 'has_express_delivery': False, 'conversion_reporting_configs': [], 'index': 1, 'speed_up': 'None', 'speed_down': 'None'}
In order to convert this string to dict, I’ve written like this.
try:
before = ast.literal_eval(log.before) if log.before else {}
except Exception as e:
print(e)
And there’s an error like this.
malformed node or string: <ast.Call object at 0x7fb3b60d94f0>
I think the problem is with the OrderedDict in the string given.
New contributor
Thomas Andersen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.