How to Check a COCO Annotation Export Before You Train a Vision Model
A practical checklist for validating a COCO-style JSON annotation file before training: structure, IDs, bounding boxes, class counts and version diffs, using free JSON tools and a short script.
Contents
Most failed training runs on image data are not model problems. They are file problems: a bounding box that falls outside the image, an annotation that points to an image that is not in the file, or a class that quietly disappeared between two exports. A ten-minute check of the annotation file catches these before you spend GPU hours.
This guide uses the COCO JSON layout because it is widely supported by labeling and training tools. The same ideas apply to other formats. Every step below works with free browser tools and one short script.
What a COCO annotation file contains
A COCO-style file is one JSON document with three main lists. images holds one entry per image, with an id, a file name, a width and a height. categories holds the classes, each with an id and a name. annotations holds one entry per labeled object, with its own id, an image_id, a category_id and a bbox.
In COCO, bbox is four numbers in pixels: [x, y, width, height], where x and y are the top-left corner. Other formats differ, which is the most common source of silent errors:
- Pascal VOC stores XML with
xmin,ymin,xmaxandymax(two corners, not a corner plus a size). - The YOLO text format stores one line per object as a class number followed by the box centre x, centre y, width and height, all scaled to the range 0 to 1.
If you convert between formats, check a few boxes by eye afterwards. A box that is shifted or squashed usually means the wrong format was assumed.
Step 1: Confirm the file is valid JSON
Large exports are sometimes truncated by a failed download or edited by hand. Paste a sample, or the whole file if it is small, into a JSON validator. It points to the line and character where parsing fails. If the file is almost valid, a JSON repair tool can fix trailing commas and similar slips, but re-export from the source tool if you can.
Next, open the file in a JSON formatter or JSON viewer and confirm the three top-level lists exist and are not empty. A file with 5,000 images and 0 annotations is a labeling or export problem, not a training problem.
Step 2: Check IDs and boxes with a short script
Browser tools are good for spot checks. For the cross-references, a few lines of Python are faster. This script reports annotations that point to a missing image or class, and boxes that leave the image:
import json
d = json.load(open("annotations.json"))
img = {i["id"]: i for i in d["images"]}
cat = {c["id"] for c in d["categories"]}
bad = [a["id"] for a in d["annotations"]
if a["image_id"] not in img or a["category_id"] not in cat]
oob = [a["id"] for a in d["annotations"]
if a["image_id"] in img and (
a["bbox"][0] < 0 or a["bbox"][1] < 0 or
a["bbox"][0] + a["bbox"][2] > img[a["image_id"]]["width"] or
a["bbox"][1] + a["bbox"][3] > img[a["image_id"]]["height"])]
print("broken references:", len(bad), "out of bounds:", len(oob)) Both numbers should be zero, or a small number you can explain. Also check that image IDs are unique and that every file name in images exists in your image folder.
Step 3: Count objects per class
Class balance affects results as much as model choice. To count objects per class without writing code, flatten the annotations list to a table. A JSON to CSV converter turns the annotations array into rows you can open in a spreadsheet, and a pivot table gives the count by category_id. Join the result to the categories list to see names.
Look for classes with very few examples, classes that have more than one ID for the same name, and categories with zero annotations. If your array is nested, a JSON flattener makes it a flat table first.
Step 4: Compare two exports
Labeling is iterative, so you will have several versions of the same file. Before you retrain, compare the new export with the previous one. A JSON diff shows added, removed and changed entries, which is useful on a small sample, and the class counts from step 3 show the large-scale change. A class that dropped by half between versions deserves a question before it reaches training.
If your new export is a different format, convert it first. Pascal VOC files are XML, and an XML to JSON converter makes them readable next to a COCO file. Our comparison of XML to JSON converters lists the mapping traps to watch for, such as attributes and repeated elements.
Step 5: Lock the structure with a schema
Once you know what a good file looks like, write that down as a JSON Schema so every future export is checked the same way. A JSON schema generator can draft one from a clean sample, and a JSON schema validator then tests new files against it. A schema will not catch a box that is wrong but well formed, so keep the script from step 2 alongside it.
When a spreadsheet and a script stop being enough
For one dataset and one person, this routine is enough. It starts to hurt when several people label, when datasets need versions, or when you need review queues and model-assisted labeling. That is where labeling platforms help. ProductListo's roundup Best Data Labeling Platforms for Computer Vision Teams (2026) compares the current options. Two profiles are worth a look if you are starting out: Roboflow combines labeling, training and deployment, and Supervisely lists migration tools for several export formats and an offline option. The wider AI and ML tools guide covers the rest of the stack.
If you track results as you iterate, the earlier UrwaTools walkthrough on tracking ML experiments with JSON and CSV pairs well with this one. Makers of labeling and ML tools can submit a tool to the ProductListo directory or read the write for us page for sponsored options.
Frequently asked questions
What is the bounding box format in COCO?
How is Pascal VOC different from COCO?
How do I know if my annotation file is broken?
Can I check an annotation file without coding?
Why compare two annotation exports?
The UrwaTools Editorial Team delivers clear, practical, and trustworthy content designed to help users solve problems efficiently. Our articles focus on accurac...