boto3
Python
JSON
S3
file-handling
Read a JSON file from S3 with Python boto3
Codemia Knowledge Hub ·
To read a JSON file from S3 with boto3, call get_object on an S3 client, read and decode the response body, and pass the string to json.loads:
data = json.loads(s3_client.get_object(Bucket=bucket, Key=key)['Body'].read().decode('utf-8'))
The rest of this page covers setup, a full example and error handling.
Prerequisites
- AWS account.
- IAM user with the necessary permissions (
AmazonS3ReadOnlyAccess). - AWS credentials: your
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEY. - Python (preferably 3.7 or higher) with
boto3installed:
Configuring credentials
- Configuration: set your AWS credentials and region with environment variables or the AWS CLI:
Or set them in code:
- IAM roles (optional): when the script runs on an AWS service such as EC2, use an IAM role for access instead.
Avoid hardcoding credentials in real applications; use environment variables or IAM roles.
Reading and parsing the JSON file
- Session and client: a session is created with your credentials, and the S3 client comes from that session.
- Fetching:
get_objectretrieves the file. - Decoding: S3 returns a binary stream, so the body must be read and decoded to a string.
- Parsing:
json.loads()converts the string into a Python dictionary.
Handling missing keys and buckets
Catch the specific S3 exceptions plus a general fallback:
| Feature | Description |
| Authentication | Use IAM roles or AWS credentials (ACCESS_KEY and SECRET_KEY). |
| Configuration | Set AWS region and credentials via environment or code. |
| Object Retrieval | Utilize s3_client.get_object to access files from an S3 bucket. |
| Data Parsing | Decode and utilize json.loads() for parsing JSON. |
| Error Management | Implement exception handling for robust code. |
Other options
boto3.resourceprovides a higher-level abstraction if you interact with S3 more extensively.- AWS Lambda with S3 triggers can process JSON files in real time as they arrive in your bucket.
Related reading
- reading a packaged file in aws lambda package
- Reading contents of a gzip file from a AWS S3 in Python
- Reading data from bucket in Google ml-engine tensorflow
- Reading data from S3 using Lambda
- Reading binary file and looping over each byte
- Reading streaming http response with Python requests library
- Receiving Email is not working in Amazon SES
- Recommended GCE service account authentication inside Docker container?
System design interview problems on this topic
- Design Dropbox: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough
- Design Youtube or Netflix: solution and walkthrough

Course
Beginner
27 lessons
10 hours
System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.