HomeLinuxConvert a String to JSON Python

Convert a String to JSON Python


JSON (JavaScript Object Notation)” is an easy-to-use data-exchange format that’s simple for people to learn/write and straightforward for machines to interpret and create/generate. In Python, you possibly can make the most of the usual JSON module to work together with JSON knowledge. To transform a Python string to JSON varied strategies resembling “json.masses()”, “ast.literal_eval()”, and so forth. are used.

Learn how to Convert a Python String to JSON?

To transform a string to JSON in Python, apply the next approaches:

Method 1: Convert a String to JSON in Python Utilizing “JSON Module”

Python supplies a built-in module referred to as “JSON” that can be utilized to transform a string to JSON. There are two strategies within the JSON module: “masses()” and “load()”. The previous methodology is utilized to take a JSON string and remodel it right into a Python object, whereas the latter methodology is used to load a JSON file and convert it to a Python object.

Instance
On this instance code, the “json.masses()” methodology is used to transform the string to JSON:

import json
json_string = ‘{“identify”: “JOSEPH”, “age”: 23, “metropolis”: “New York”}’
json_obj = json.masses(json_string)
print(json_obj)

Within the above code, the “json.masses()” methodology takes the initialized JSON string as its argument and retrieves the JSON dictionary object.

Output

As seen, the offered string has been transformed into JSON appropriately.

Method 2: Convert a String to JSON in Python Utilizing the “ast Module”

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

Instance
Let’s overview the below-provided instance:

import ast
json_string = ‘{“Identify”: “Joseph”, “Age”: 23, “Metropolis”: “New York”}’
json_dict = ast.literal_eval(json_string)
print(json_dict)

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

Output

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

Method 3: Convert a String to JSON in Python Utilizing the “eval()” Perform

The “eval()” perform in Python may also be used to guage a string containing a JSON object and convert it to a Python dictionary which could be demonstrated within the beneath instance.

Instance
Undergo the next code snippet:

json_string = ‘{“Identify”: “Joseph”, “Age”: 23, “Peak”: 5.7}’
json_dict = eval(json_string)
print(json_dict)

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

Output

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

Be aware: As this methodology poses safety dangers, it isn’t advisable.

Conclusion

The JSON module capabilities, the “ast” module perform, or the “eval()” perform can be utilized to transform a string to JSON in Python. The “masses()” methodology is utilized to load a JSON string object and remodel it right into a Python object. The “ast.literal_eval()” and “eval()” capabilities may also be used to transform a string right into a JSON object. This Python information offered varied methods to transform a string to JSON utilizing quite a few examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments