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
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read