Python

Convert a String to JSON Python

JSON (JavaScript Object Notation)” is an easy-to-use data-exchange format that is straightforward for humans to read/write and easy for machines to interpret and create/generate. In Python, you can utilize the standard JSON module to interact with JSON data. To convert a Python string to JSON various methods such as “json.loads()”, “ast.literal_eval()”, etc. are used.

How to Convert a Python String to JSON?

To convert a string to JSON in Python, apply the following approaches:

Approach 1: Convert a String to JSON in Python Using “JSON Module”

Python provides a built-in module called “JSON” that can be used to convert a string to JSON. There are two methods in the JSON module: “loads()” and “load()”. The former method is utilized to take a JSON string and transform it into a Python object, while the latter method is used to load a JSON file and convert it to a Python object.

Example
In this example code, the “json.loads()” method is used to convert the string to JSON:

import json
json_string = '{"name": "JOSEPH", "age": 23, "city": "New York"}'
json_obj = json.loads(json_string)
print(json_obj)

In the above code, the “json.loads()” method takes the initialized JSON string as its argument and retrieves the JSON dictionary object.

Output

As seen, the provided string has been converted into JSON appropriately.

Approach 2: Convert a String to JSON in Python Using the “ast Module”

The “ast.literal_eval()” method of the “ast module” can be used to evaluate a string containing a JSON object and convert it to a Python dictionary.

Example
Let’s overview the below-provided example:

import ast
json_string = '{"Name": "Joseph", "Age": 23, "City": "New York"}'
json_dict = ast.literal_eval(json_string)
print(json_dict)

In the above code, the “ast.literal_eval()” method takes the JSON string as its argument and converts it into a JSON dictionary object.

Output

As seen, the given string has been converted into JSON.

Approach 3: Convert a String to JSON in Python Using the “eval()” Function

The “eval()” function in Python can also be used to evaluate a string containing a JSON object and convert it to a Python dictionary which can be demonstrated in the below example.

Example
Go through the following code snippet:

json_string = '{"Name": "Joseph", "Age": 23, "Height": 5.7}'
json_dict = eval(json_string)
print(json_dict)

In this code, the “eval()” function takes the JSON string as its argument and converts it into JSON.

Output

This output implies that the string has been converted into JSON appropriately.

Note: As this method poses security risks, it is not recommended.

Conclusion

The JSON module functions, the “ast” module function, or the “eval()” function can be used to convert a string to JSON in Python. The “loads()” method is utilized to load a JSON string object and transform it into a Python object. The “ast.literal_eval()” and “eval()” functions can also be used to convert a string into a JSON object. This Python guide presented various ways to convert a string to JSON using numerous examples.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.