How to Convert Python String to JSON

To convert string to json in Python, use the json.loads() function. The json.loads() is a built-in function that accepts a valid json string and returns a dictionary to access all elements.

The process of converting json data to Python objects is called deserialization.

The json.loads() function parses valid JSON string into a dictionary.

To use the json.loads() method, import the json package offered by Python at the head of the file.

# app.py

import json

json_string = '''
{
  "students": [
    {
      "name": "Millie Brown",
      "active": true,
      "rollno": 11
    },
    {
      "name": "Sadie Sink",
      "active": true,
      "rollno": 10
    }
  ]
}
'''
print(json_string)
print("The type of object is: ", type(json_string))
stud_obj = json.loads(json_string)
print(stud_obj)
print("The type of object is: ", type(stud_obj))
json_obj = json.dumps(stud_obj)
print(json_obj)
print("The type of object is: ", type(json_obj))

Output

{
  "students": [
    {
      "name": "Millie Brown",
      "active": true,
      "rollno": 11
    },
    {
      "name": "Sadie Sink",
      "active": true,
      "rollno": 10
    }
  ]
}

The type of object is:  <class 'str'>
{'students': [{'name': 'Millie Brown', 'active': True, 'rollno': 11}, 
              {'name': 'Sadie Sink', 'active': True, 'rollno': 10}]}
The type of object is:  <class 'dict'>
{"students": [{"name": "Millie Brown", "active": true, "rollno": 11}, 
              {"name": "Sadie Sink", "active": true, "rollno": 10}]}
The type of object is:  <class 'str'>

In this example, we first convert the json string to the Python dictionary object using json.loads() method and then converting the dictionary to string using json.dumps() method.

Why do you need to convert the string to json format?

Converting a string to a JSON object in Python enables you to easily modify the data stored in the string.

JSON (JavaScript Object Notation) is a popular data format used for storing data in a structured manner. By converting a string to a JSON object, you can access and modify the data in the object, as well as use the object in any JSON-related processes or functions.

Data Conversion from JSON to Python Objects

  1. object => dict
  2. array => list
  3. string => str
  4. number (int) => int
  5. number (real) => float
  6. true => True
  7. false => False
  8. null => None

Difference between json.load() and json.loads()

The main difference between json.load() and json.loads() function that json.load() function reads the JSON document from a file and the json.loads() function is used to convert the JSON String document into the Python dictionary.

The json.load() function can deserialize a file itself.

The json.loads() function deserialize a string.

Conclusion

To transform a string to JSON data in Python, use the json.loads() method that comes with a built-in json package.

Related posts

Python string to int

Python string to list

Python string to array

Python string to datetime

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.