Convert string to JSON Python
In this post, you will learn how to convert string to JSON using the Python programming language.
In the development process, we may face some scenarios where there may be a need to perform a data conversion operation. A string is a sequence of characters. This must be enclosed within single quotes ('...') and double quotes ("..."). Strings are immutable, indexable, and iterable.
JSON (JavaScript Object Notation) is a lightweight, open standard file format. It is an array data type, consisting of attribute-value pairs. It is easy to read and write for humans. It is used primarily to transmit data between a web application and a server. JSON is popular among developers for data serialization. It is so popular that every modern programming language has methods to generate and parse JSON formatted data.
Import JSON
Before we get started working on JSON, we first need to import the json library. This library contains all the necessary methods to work with JSON.
import JSON
Convert Python string to JSON using json.loads
The json.loads() function is used to convert a strings to JSON in Python. The JSON library contains a json.loads() method that accepts a valid JSON string and returns a dictionary to access all elements.
Syntax of the json.loads()
json.loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
# converting string to json
# using json.loads
import json
# inititialising json object
emp_string = '{"first_name": "Priska", "last_name": "Kashyap", "department": "Developer"}'
# Printing initial json
emp_string = json.dumps(emp_string)
print ("Initial Data: ", emp_string)
print ("Type of emp_string: ", type(emp_string))
# Converting string to json
data = json.loads(emp_string)
# printing final result
print ("Converted Data: ", str(data))
print ("Type of data: ", type(data))
Output of the above code:
Initial Data: "{\"first_name\": \"Priska\", \"last_name\": \"Kashyap\", \"department\": \"Developer\"}"
Type of emp_string: <class 'str'>
Converted Data: {"first_name": "Priska", "last_name": "Kashyap", "department": "Developer"}
Type of data: <class 'str'>
Convert Python String to JSON using eval() Function
We can also use the eval() function to convert Python strings to JSON. Python provides in-built function eval() that dynamically evaluates expressions from a string-based or compiled-code-based input. If you pass a string as a parameter to eval(), then the function parses it, compiles it to bytecode, and evaluates it as a Python expression.
# converting string to json
# using json.loads
import json
# inititialising json object
student_string = '{"first_name": "Priska", "last_name": "Kashyap", "department": "Developer"}'
# Printing initial json
student_string = json.dumps(student_string)
print ("Initial Data: ", student_string)
print ("Type of stu_string: ", type(student_string))
# Converting string to json
data = eval(student_string)
# Printing final result
print ("Converted Data: ", str(data))
print ("Type of data: ", type(data))
Output of the above code:
Initial Data: "{\"first_name\": \"Priska\", \"last_name\": \"Kashyap\", \"department\": \"Developer\"}"
Type of stu_string: <class 'str'>
Converted Data: {"first_name": "Priska", "last_name": "Kashyap", "department": "Developer"}
Type of data: <class 'str'>
Related Articles
Convert MySQL query result to JSON in PythonInsert JSON data into MongoDB using Python
Convert MongoDB Document to JSON using Python
Python read JSON from URL requests
Python convert XML to JSON
Convert JSON to CSV using Python
Python JSON Tutorial - Create, Read, Parse JSON
Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml