Open In App

Decoding Json String with Single Quotes in Python

Last Updated : 12 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Using ast.literal_eval() Function
  2. Using json.loads() Function
  3. 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'])

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

Output
Decoding 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"])

Output
Decoding Using single quotes:
The address of Ragu is {'street': 'Gradenl', 'city': 'Pune'}

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads