Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Instruction on check_json_files.py script


Owner: Vadim Rudakov, rudakow.wadim@gmail.com
Version: 0.1.0
Birth: 2026-01-21
Last Modified: 2026-01-21


1. Architectural Overview: The SVA Principle

This script validates JSON files for syntax errors using Python’s standard library json module.

This tool is designed to serve as a quality gate in CI/CD, preventing commits with malformed JSON files that could break configuration, data pipelines, or application behavior.

It adheres to the Smallest Viable Architecture (SVA) principle.

2. Key Capabilities & Logic

A. JSON Syntax Validation

The script validates:

CheckDescription
Bracket MatchingVerifies all { and [ have matching } and ]
String QuotingEnsures strings use double quotes (not single quotes)
Key QuotingVerifies all object keys are quoted strings
Trailing CommasDetects invalid trailing commas in arrays/objects
UTF-8 EncodingValidates files are properly UTF-8 encoded

B. Directory Exclusions

When scanning directories, the script automatically excludes:

DirectoryReason
.gitVersion control internals
.venv, venvPython virtual environments
__pycache__Python bytecode cache
node_modulesNode.js dependencies
.ipynb_checkpointsJupyter notebook checkpoints
build, distBuild artifacts

3. Technical Architecture

The script is organized into specialized classes to maintain clarity:

ClassResponsibility
JsonValidatorParse and validate JSON syntax using json.loads()
FileFinderRecursive file discovery for *.json files
ReporterOutput formatting and exit code handling
JsonValidatorCLIArgument parsing and main orchestration
JsonErrorNamedTuple storing validation errors (file, line, message)

4. Operational Guide

Configuration Reference

  • Primary Script: tools/scripts/check_json_files.py

  • Pre-commit Config: .pre-commit-config.yaml

  • CI Config: .github/workflows/quality.yml

Command Line Interface

check_json_files.py [files...] [--verbose]
ArgumentDescriptionDefault
filesOne or more JSON files to validateCurrent directory (recursive)
--verboseShows detailed logs of validated files and resultsFalse

Exit Codes:

  • 0 = All JSON files valid

  • 1 = Validation errors found

Manual Execution Commands

Run these from the repository root using uv for consistent environment resolution:

TaskCommand
Check Specific Filesuv run tools/scripts/check_json_files.py file1.json file2.json
Full Directory Scanuv run tools/scripts/check_json_files.py --verbose
Pre-commit Modeuv run tools/scripts/check_json_files.py path/to/staged/file.json

Examples

cd ../../../
ls
0_intro       CLAUDE.md       misc            RELEASE_NOTES.md         uv.lock
ai_system     CONVENTIONS.md  mlops           security
architecture  LICENSE-CODE    myst.yml        test_commit_prompt.json
_build        LICENSE-DOCS    pyproject.toml  todo
CHANGELOG     LICENSE.md      README.md       tools
  1. Check specific files:

env -u VIRTUAL_ENV uv run tools/scripts/check_json_files.py ai_system/3_prompts/consultants/devops_consultant.json
  1. Check with verbose output:

env -u VIRTUAL_ENV uv run tools/scripts/check_json_files.py --verbose ai_system/3_prompts/consultants/devops_consultant.json
Checking 1 file(s)...
  OK: ai_system/3_prompts/consultants/devops_consultant.json

All JSON files are valid.

5. Validation Layers

Layer 1: Local Pre-commit Hook

The first line of defense runs automatically during the git commit process to prevent malformed JSON from entering version control.

Pre-commit Configuration:

- id: check-json-files
  name: Check JSON Files
  entry: uv run --active tools/scripts/check_json_files.py
  language: system
  files: \.json$
  pass_filenames: true
  exclude: ^(tools/tests/|\.vscode/)
  • Scope: Validates all staged .json files

  • pass_filenames: true: Receives file list from git, enabling targeted checking

  • Exclusion: Test fixtures and VS Code settings are excluded

Layer 2: GitHub Action (Continuous Integration)

The CI pipeline in quality.yml runs the test suite when relevant files change:

- name: Get changed json-validation files
  id: changed-json-validation
  uses: tj-actions/changed-files@v45
  with:
    files: |
      tools/scripts/check_json_files.py
      tools/tests/test_check_json_files.py

- name: Run JSON Validation Tests
  if: steps.changed-json-validation.outputs.any_changed == 'true' || steps.changed-paths.outputs.any_changed == 'true'
  run: uv run pytest tools/tests/test_check_json_files.py

Note: paths.py is detected separately because it’s a shared dependency across multiple test suites.

Layer 3: Logic Tests Pre-commit Hook

A meta-check ensures the validation logic remains sound:

- id: test-check-json-files
  name: Test JSON validation script
  entry: uv run --active pytest tools/tests/test_check_json_files.py
  language: system
  pass_filenames: false
  files: ^tools/(scripts/check_json_files\.py|scripts/paths\.py|tests/test_check_json_files\.py)$

This triggers whenever the script, its tests, or shared configuration change.

6. Test Suite Documentation

The script is accompanied by a comprehensive test suite (test_check_json_files.py) that ensures reliability across different patterns and edge cases.

Test Classes and Coverage

Test ClassPurpose
TestJsonValidatorValid JSON, syntax errors, encoding errors, line number reporting
TestFileFinderRecursive discovery, directory exclusion, nested files
TestReporterExit codes, output formatting, verbose mode
TestJsonValidatorCLIIntegration tests for CLI modes and arguments

Key Test Scenarios

  • Valid JSON: Objects, arrays, nested structures, Unicode content

  • Invalid JSON: Missing brackets, trailing commas, single quotes, unquoted keys

  • Line Numbers: Correct line reporting for multi-line files

  • Binary Files: Gracefully handled with error message

  • Missing Files: Warning issued, non-blocking

  • Empty Files: Treated as valid (skipped)

  • Directory Exclusions: .git, node_modules, __pycache__, etc.

Running the Tests

To run the full suite, execute from the repository root:

$ uv run pytest tools/tests/test_check_json_files.py
env -u VIRTUAL_ENV uv run pytest tools/tests/test_check_json_files.py -q
......................................................                   [100%]
54 passed in 0.05s
env -u VIRTUAL_ENV uv run pytest tools/tests/test_check_json_files.py --cov=. --cov-report=term-missing -q
......................................................                   [100%]
================================ tests coverage ================================
_______________ coverage: platform linux, python 3.13.11-final-0 _______________

Name                                   Stmts   Miss  Cover   Missing
--------------------------------------------------------------------
tools/scripts/check_json_files.py         92      3    97%   108, 192, 202
tools/tests/test_check_json_files.py     277      0   100%
--------------------------------------------------------------------
TOTAL                                    369      3    99%
54 passed in 0.14s