Decoding Json String with Single Quotes in Python
Last Updated :
12 Mar, 2024
We are given a JSON string with single quotes and our task is to parse JSON with single quotes in Python. In this article, we will see how we can parse JSON with single quotes in Python.
Example:
Input: json_string = "{'name': 'Ragu','age': 30,'salary':30000,'address': { 'street': 'Gradenl','city': 'Pune','state': 'Maharastra'}}"
Output: The address of Ragu is {'street': 'Gradenl', 'city': 'Pune', 'state': 'Maharastra'}
Explanation: Here, we have parsed the address from the given single quoted JSON string.
Parse JSON with Single Quote in Python
Below are some of the ways by which we can parse single quotes JSON in Python:
- Using ast.literal_eval() Function
- Using json.loads() Function
- Using eval() Function
Using ast.literal_eval() Function
In this example, a JSON string with single quotes is decoded into a Python dictionary using the ast.literal_eval()
function. The resulting dictionary, my_dict
, allows easy access to the nested data, such as the address, demonstrating how to handle single-quote formatted JSON strings in Python.
Python3
# code
import ast
json_string = "{'name': 'Ragu','age': 30,'salary':30000,'address': { 'street': 'Gradenl','city': 'Pune','state': 'Maharastra'}}"
my_dict = ast.literal_eval(json_string)
print("Decoding Using single quotes:")
print("The address of %s is" % my_dict['name'], my_dict['address'])
OutputDecoding Using single quotes:
The address of Ragu is {'street': 'Gradenl', 'city': 'Pune', 'state': 'Maharastra'}
Using json.loads() Function
In this example, a JSON string with single quotes is decoded using json.loads()
after replacing single quotes with double quotes. The resulting Python dictionary, my_dict
, is then accessed to print information about a person's name and address, demonstrating a workaround for handling single-quote formatted JSON strings in Python.
Python3
import json
json_string = "{'name': 'Ragu','age': 30,'salary':30000,'address': { 'street': 'Gradenl','city': 'Pune'}}"
my_dict = json.loads(json_string.replace("'", "\""))
print("Decoding Using single quotes:")
print("The address of %s is" % my_dict['name'], my_dict['address'])
OutputDecoding Using single quotes:
The address of Ragu is {'street': 'Gradenl', 'city': 'Pune'}
Using eval() Function
In this example, a JSON-like string with single quotes is converted to a dictionary using the eval()
function, and the address of the person named "Ragu" is then printed using the obtained dictionary. However, using eval
can pose security risks, and it's generally safer to use the json.loads
method for such conversions.
Python3
json_string = "{'name': 'Ragu','age': 30,'salary':30000,'address': { 'street': 'Gradenl','city': 'Pune'}}"
# converting the json singe quote string to dictionary
my_dict = eval(json_string)
print("Decoding Using single quotes:")
print("The address of %s is" % my_dict["name"], my_dict["address"])
OutputDecoding Using single quotes:
The address of Ragu is {'street': 'Gradenl', 'city': 'Pune'}
Similar Reads
Single Vs Double Quotes in Python Json JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used for data storage and exchange between web servers and clients. In Python, dealing with JSON is a common task, and one of the decisions developers face is whether to use single or double quotes for string represent
3 min read
Printing String with double quotes - Python Printing a string with double quotes means displaying a string enclosed in double quotes (") as part of the output. This can be helpful when we want to make it clear that the text itself is a string or when quotes are essential to the context.Using Escape Characters (\")Escape the double quotes insi
2 min read
Python - Triple quote String concatenation Sometimes, while working with Python Strings, we can have a problem in which we need to perform concatenation of Strings which are constructed by Triple quotes. This happens in cases we have multiline strings. This can have applications in many domains. Let us discuss certain ways in which this task
4 min read
Convert JSON to string - Python Data is transmitted across platforms using API calls. Data is mostly retrieved in JSON format. We can convert the obtained JSON data into String data for the ease of storing and working with it. Python provides built-in support for working with JSON through the json module. We can convert JSON data
2 min read
Avoiding Quotes while Printing Strings When printing strings in Python, quotes are usually omitted in standard output. However, when printing lists or dictionaries, string values appear enclosed in quotes. For example, consider the list ["Paris", "London", "New York"]. If printed directly, it appears as ['Paris', 'London', 'New York'] wi
2 min read