Learn how to convert SRT (SubRip Text) files into structured JSON format using Python. We explain the common pitfalls and provide a straightforward solution to get your JSON output correctly formatted.
---
This video is based on the question https://stackoverflow.com/q/67102430/ asked by the user 'sauron_08' ( https://stackoverflow.com/u/8815141/ ) and on the answer https://stackoverflow.com/a/67102515/ provided by the user 'Blast Off Bronze' ( https://stackoverflow.com/u/15600610/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: SRT (SubRip Text) to JSON using Python
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Converting SRT to JSON Using Python: A Simple Guide
If you’re a programmer trying to convert subtitles from SRT format to JSON, you may encounter unexpected output, such as unwanted newline characters. In this guide, we will break down the process of converting SRT files into JSON while highlighting common pitfalls and providing solutions to ensure you get a clean and structured output.
Understanding the SRT Format
SubRip Subtitle (SRT) is one of the widely-used formats for video subtitles. It consists of a series of entries, each containing a sequence number, the timing for when the subtitle is displayed, and the actual subtitle text. Here is what a typical SRT entry looks like:
[[See Video to Reveal this Text or Code Snippet]]
Your goal is to take these entries and convert them into a JSON format that can easily be manipulated. Each entry may contain important fields like startTime, endTime, and ref (the text of the subtitle).
Common Issue: Incorrect JSON Output
When attempting to convert SRT to JSON in Python, you might find that the output format isn't correct—often displaying unwanted characters like \n. This issue can arise from mistakenly converting a JSON object to a string before writing it to a file.
Step-by-Step Solution
Step 1: Read the SRT File
You'll first need to read the contents of your SRT file. We use regular expressions to capture the details of each subtitle entry. The following code snippet demonstrates this:
[[See Video to Reveal this Text or Code Snippet]]
Step 2: Convert Times to Seconds
Next, we need a function that converts timestamp strings into seconds. This allows for easier calculations later on.
[[See Video to Reveal this Text or Code Snippet]]
Step 3: Create a List of Dictionaries
We then create a list of dictionaries, where each dictionary corresponds to an individual entry of the SRT file. This includes startTime, endTime, and the subtitle text (ref):
[[See Video to Reveal this Text or Code Snippet]]
Step 4: Write to JSON Properly
The key to resolving the newline issue is to write the list of dictionaries directly using json.dump instead of converting it to a string first. Here’s how you can do that:
[[See Video to Reveal this Text or Code Snippet]]
Final Output
By following the above steps, your data.json should be formatted correctly, without any unwanted newline characters. Here’s an example of how the output might look:
[[See Video to Reveal this Text or Code Snippet]]
Conclusion
In this guide, we've walked through the steps to convert SRT files into a properly formatted JSON using Python. Remember, the key takeaway is to use json.dump directly on the list of dictionaries instead of converting it to a string first, which avoids unnecessary formatting issues. Now you can easily manipulate subtitle data in JSON format for your applications. Happy coding!
On this page of the site you can watch the video online Converting SRT to JSON Using Python with a duration of hours minute second in good quality, which was uploaded by the user vlogize 26 May 2025, share the link with friends and acquaintances, this video has already been watched 27 times on youtube and it was liked by like viewers. Enjoy your viewing!