Skip to main content

Building a JSON Report in Python

Updated 2 min readMarkdown
Building a JSON Report in Python

1. Understanding the code

The following Python code creates a report using a dictionary, which can later be converted into JSON format.

report = {
    "filename": uploaded_file.name,
    "number_of_pages": page_count,
    "metadata": metadata,
}

Please note that this code creates a Python dictionary, not a JSON file by itself. It can be used in a regular Python script or a Streamlit application, provided the required variables have been defined.

2. Breaking down each field

The report variable is a Python dictionary that organizes information into key-value pairs. Each key identifies a piece of information, and its corresponding value provides the data associated with it.

In this dictionary, the keys are "filename", "number_of_pages", and "metadata". The corresponding values are uploaded_file.name, page_count, and metadata.

"filename": uploaded_file.name

  • Key: "filename"
  • Value: uploaded_file.name, which contains the name of the uploaded file. The uploaded_file object must support the .name attribute.

"number_of_pages": page_count

  • Key: "number_of_pages"
  • Value: page_count, which contains the total number of pages in the document.

"metadata": metadata

  • Key: "metadata"
  • Value: metadata, which contains additional document information, such as the author, title, or creation date.

The variables uploaded_file, page_count, and metadata must be defined before creating the report.

Example output:

The dictionary could contain the following information:

{
    "filename": "sample.pdf",
    "number_of_pages": 5,
    "metadata": {
        "title": "Sample Document",
        "author": "John Doe"
    }
}

The values shown above are examples. Your actual output depends on the uploaded file and the metadata you extract.


This mini-guide was originally published on PDF Python Hub, where you can find more in-depth Python resources for working with PDFs.

About the author

  • PDF Python Hub is dedicated to teaching PDF programming, automation, and engineering with Python through practical, project-based courses. Whether you're a complete beginner or an experienced Python developer, the goal is to help you build the skills needed to work with PDF documents confidently—from reading and extracting data to building enterprise document processing and AI-powered workflows. What You'll Learn The curriculum covers every stage of PDF processing, including: -PDF fundamentals -Document extraction -PDF manipulation -Forms, tables, and images -OCR and scanned documents -Security and redaction -Enterprise automation -AI-powered document processing How You'll Learn Every course is designed to be: -Beginner-friendly with clear, step-by-step explanations -Focused on practical, real-world applications -Built around hands-on projects and assessments -Structured as a complete learning path from beginner to expert Our Mission Our mission is simple: To make PDF processing with Python accessible, practical, and enjoyable by teaching real skills that can be applied to everyday automation tasks and professional software development. Whether you're learning for your career, your business, or your own projects, you'll gain the knowledge and confidence to build powerful PDF automation solutions with Python. Learn more: https://payhip.com/PDFPythonHub/blog/news

No comments yet. Be the first to comment.