Convert String to JSON Object - Python
Last Updated :
15 Apr, 2025
The goal is to convert a JSON string into a Python dictionary, allowing easy access and manipulation of the data. For example, a JSON string like {"name": "John", "age": 30, "city": "New York"} can be converted into a Python dictionary, {'name': 'John', 'age': 30, 'city': 'New York'}, which allows you to access values by their keys. Let's explore different methods to do this efficiently.
Using json.loads()
json.loads() method is the most commonly used function for parsing a JSON string and converting it into a Python dictionary. The method takes a string containing JSON data and deserializes it into a Python object. This is the most efficient way to handle JSON data in Python.
Python
import json
s = '{"name": "John", "age": 30, "city": "New York"}'
res= json.loads(s)
print(type(res),res)
Output<class 'dict'> {'name': 'John', 'age': 30, 'city': 'New York'}
Explanation: loads() function stands for "load string," and it parses the JSON string and converts it into a dictionary. This resulting dictionary is assigned to the variable res.
Using ast.literal_eval()
ast.literal_eval() function safely evaluates a string containing Python literals (including JSON-like structures) and converts it into an equivalent Python object. It’s safer than eval() because it only allows expressions containing Python literals, such as dictionaries, lists, numbers and strings.
Python
import ast
s = '{"name": "John", "age": 30, "city": "New York"}'
res = ast.literal_eval(s)
print(type(res), res)
Output<class 'dict'> {'name': 'John', 'age': 30, 'city': 'New York'}
Explanation: ast.literal_eval() function parses the string and converts it into a dictionary, ensuring that only literal expressions are evaluated.
Using eval()
eval() function evaluates a string as a Python expression and returns the result. While powerful, eval() is unsafe when working with untrusted input, as it can execute arbitrary code. It is generally not recommended for parsing JSON due to security risks but can still be used to convert a string that follows Python's literal syntax into a dictionary.
Python
s = '{"name": "John", "age": 30, "city": "New York"}'
res = eval(s)
print(type(res), res)
Output<class 'dict'> {'name': 'John', 'age': 30, 'city': 'New York'}
Explanation: eval(s) takes the string s, evaluates it as a Python expression and returns the result as a Python object.
Similar Reads
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
Convert List Of Tuples To Json String in Python We have a list of tuples and our task is to convert the list of tuples into a JSON string in Python. In this article, we will see how we can convert a list of tuples to a JSON string in Python. Convert List Of Tuples To Json String in PythonBelow, are the methods of Convert List Of Tuples To Json St
3 min read
Python To Generate Dynamic Nested Json String JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Python, working with dynamic nested JSON strings is a common task, especially when dealing with complex data structures. In this artic
3 min read
Convert JSON to GeoJSON Python GeoJSON has become a widely used format for representing geographic data in a JSON-like structure. If you have data in a standard JSON format and need to convert it into GeoJSON for mapping or analysis, Python provides several methods to make this conversion seamless. In this article, we will explor
4 min read
Convert CSV to JSON using Python Converting CSV to JSON using Python involves reading the CSV file, converting each row into a dictionary and then saving the data as a JSON file. For example, a CSV file containing data like names, ages and cities can be easily transformed into a structured JSON array, where each record is represent
2 min read