Decoding JSON String When There are Single Quotes


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.

Consider the following JSON string:

{
"name": "John",
"age": 30,
"hobbies": ["reading", "swimming", "playing guitar"],
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zipcode": "10001"
},
"description": "John's favorite quote is 'Carpe diem.'"
}

This JSON string is perfectly valid and can be decoded using Python’s built-in json module with the loads() function:

import json

json_str = '{"name": "John", "age": 30, "hobbies": ["reading", "swimming", "playing guitar"], "address": {"street": "123 Main St", "city": "New York", "state": "NY", "zipcode": "10001"}, "description": "John\'s favorite quote is \'Carpe diem.\'"}'

data = json.loads(json_str)

print(data)

The output is:

{'name': 'John', 'age': 30, 'hobbies': ['reading', 'swimming', 'playing guitar'], 'address': {'street': '123 Main St', 'city': 'New York', 'state': 'NY', 'zipcode': '10001'}, 'description': "John's favorite quote is 'Carpe diem.'"}

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.

For example, consider the following JSON string:

{
'name': 'John',
'age': 30,
'hobbies': ['reading', 'swimming', 'playing guitar'],
'address': {
'street': '123 Main St',
'city': 'New York',
'state': 'NY',
'zipcode': '10001'
},
'description': 'John\'s favorite quote is "Carpe diem."'
}

If we try to decode this string using the json module, we will get a JSONDecodeError:

import json

json_str = "{'name': 'John', 'age': 30, 'hobbies': ['reading', 'swimming', 'playing guitar'], 'address': {'street': '123 Main St', 'city': 'New York', 'state': 'NY', 'zipcode': '10001'}, 'description': 'John\\'s favorite quote is \"Carpe diem.\"'}"

data = json.loads(json_str)

print(data)

with error:

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'])


it will work!


Author: robot learner
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 !
  TOC