JSON (JavaScript Object Notation) is a popular format used for transmitting data between applications. It’s a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. However, JSON data is typically enclosed in double quotes, which can be problematic when dealing with data that contains single quotes.
However, if the JSON string uses single quotes instead of double quotes, the json module will raise a JSONDecodeError exception because the string is not valid JSON.
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.8/json/__init__.py", line 357, in loads return _default_decoder.decode(s) File "/usr/lib/python3.8/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.
Now the ast function come to rescue, if we do this instead:
import ast
# A string with single quotes my_string = "{'name': 'John', 'age': 30, 'city': 'New York'}"
# Use ast.literal_eval() to evaluate the string into a dictionary my_dict = ast.literal_eval(my_string)
# Print the resulting dictionary print(my_dict['name'])
Reprint policy:
All articles in this blog are used except for special statements
CC BY 4.0
reprint policy. If reproduced, please indicate source
robot learner
!