Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Python JSON – Read a JSON String

Learn to read JSON string in Python with the help of json.loads() method which converts a given JSON string into a Python object. For quick reference, below is the code which reads a JSON string into a Python object. 1. json.loads() Method The json.loads() deserializes a given JSON …

Lokesh Gupta

October 2, 2022

Python JSON
JSON Files, Python Basics
json editor

Learn to read JSON string in Python with the help of json.loads() method which converts a given JSON string into a Python object.

For quick reference, below is the code which reads a JSON string into a Python object.

import json

#JSON String
json_string = '{"name": "Lokesh", "age": 38, "locations": ["India", "USA"]}'

# Convert json string to object
json_dict = json.loads(json_string)

# Ready to use object further into the program
print(json_dict)

1. json.loads() Method

The json.loads() deserializes a given JSON string into a python dictionary/list object using these conversion rules.

JSON Python

object

dict

array

list

string

str

number (int)

int

number (real)

float

true

True

false

False

null

None

If the data being deserialized is not a valid JSON document, a JSONDecodeError will be raised.

2. Python Read JSON String Examples

Example 1: Reading a JSON String to Python List

In given example, we are reading a list of JSON objects into a Python list.

import json

# JSON String
json_string = '[{"name": "Lokesh", "age": 38}, {"name": "Brian", "age": 48}]'

# Read into Python list
py_list = json.loads(json_string)

print(type(py_list))
print(py_list)

Program output.

<class 'list'>
[{'name': 'Lokesh', 'age': 38}, {'name': 'Brian', 'age': 48}]

Example 2: Reading a JSON String to Python Dictionary

In given example, we are reading a list of JSON objects into a Python list.

import json

# JSON String
json_string = '{"name": "Lokesh", "age": 38}, {"name": "Brian", "age": 48}'

# Read into Python list
py_list = json.loads(json_string)

print(type(py_list))
print(py_list)

Program output.

<class 'dict'>
{'name': 'Lokesh', 'age': 38, 'locations': ['India', 'USA']}

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Python JSON

  • Read JSON file
  • Read JSON String
  • Write JSON to File
  • Append JSON to File
  • Custom Serialization
  • Custom Deserialization

Table of Contents

  • 1. json.loads() Method
  • 2. Python Read JSON String Examples
      • Example 1: Reading a JSON String to Python List
      • Example 2: Reading a JSON String to Python Dictionary
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Python JSON – Read a JSON file

Next

Python – Write JSON to a File

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap