In this article, we will see different ways to convert string to JSON in Python this process is called serialization. JSON module provides functions for encoding (serializing) Python objects into JSON strings and decoding (deserializing) JSON strings into Python objects.
- Encoding (Serializing) JSON: If you have a Python object and want to convert it to a JSON string, you can use the json.dumps() function. It takes the Python object as input and returns a JSON string.
- Decoding (Deserializing) JSON: If you have a JSON string and want to convert it to a Python object, you can use the json.loads() function. It takes the JSON string as input and returns a Python object.
Convert String to JSON Object in Python
Below are methods to convert Python strings to JSON objects:
- Using json.loads() Method
- Using eval() Method
- Using ast.literal_eval() Method
Convert String to JSON Using json.loads() Method in Python
In this example, we will convert a dictionary to a JSON object using the json.dump() function. Then we will convert the string representation of a JSON object to a dictionary using json.loads() method.
Python3
import json # initialising json object ini_string = { 'nikhil' : 1 , 'akash' : 5 , 'manjeet' : 10 , 'akshat' : 15 } # printing initial json ini_string = json.dumps(ini_string) print ( "initial 1st dictionary" , ini_string) print ( "type of ini_object" , type (ini_string)) # converting string to json final_dictionary = json.loads(ini_string) # printing final result print ( "final dictionary" , str (final_dictionary)) print ( "type of final_dictionary" , type (final_dictionary)) |
Output:
initial 1st dictionary {'manjeet': 10, 'nikhil': 1, 'akshat': 15, 'akash': 5}
type of ini_object <type 'str'>
final dictionary {'nikhil': 1, 'manjeet': 10, 'akshat': 15, 'akash': 5}
type of final_dictionary <type 'dict'>
Convert String to JSON Using eval() Method in Python
The eval() function in Python evaluates the expression input as a Python expression and executes the Python expression (code) within the program.
Example
In this example, we are converting Python string representation to a dictionary by using eval() method.
Python3
# initialising json object string ini_string = """{'nikhil': 1, 'akash' : 5, 'manjeet' : 10, 'akshat' : 15}""" # printing initial json print ( "initial 1st dictionary" , ini_string) print ( "type of ini_object" , type (ini_string)) # converting string to json final_dictionary = eval (ini_string) # printing final result print ( "final dictionary" , str (final_dictionary)) print ( "type of final_dictionary" , type (final_dictionary)) |
Output:
initial 1st dictionary {'nikhil': 1, 'akash' : 5, 'manjeet' : 10, 'akshat' : 15}
type of ini_object <class 'str'>
final dictionary {'nikhil': 1, 'manjeet': 10, 'akash': 5, 'akshat': 15}
type of final_dictionary <class 'dict'>
Convert String to JSON Using ast.literal_eval() Method
The ast.literal_eval() method is a part of the ast(abstract syntax tree) module in Python. This function evaluates an expression node, a string made up of a literal, and converts it to a Python dictionary object.
Example
Here, we will use the ast.literal_eval() method of the past module to convert a string representation of the dictionary to a Python dictionary. The ast.literal_eval function parses the string and creates a Python dictionary object with the same properties and values. The resulting dictionary object has a type of dict. We can then access the properties of the dictionary object using standard dictionary access notation (e.g. dict_obj[“name”]).
Python3
import ast # initialize the string to be converted string = '{"name": "John", "age": 30, "city": "New York"}' # use the ast.literal_eval function to parse the string and create a dictionary object dict_obj = ast.literal_eval(string) # printing final result print ( "Initial string dictionary: " ,string) print ( "Final dictionary: " ,dict_obj) print ( type (dict_obj)) |
Output:
Initial string dictionary: {"name": "John", "age": 30, "city": "New York"}
Final dictionary: {'name': 'John', 'age': 30, 'city': 'New York'}
<class 'dict'>