for Python
Parse and work with JSON6 files in your Python applications.
TL;DR: Install with pip install json6, then use json6.load() and json6.loads() just like the standard json module.
Installation
The most popular JSON6 library for Python is json6, available on PyPI.
pip
pip install json6pipenv
pipenv install json6Poetry
poetry add json6uv
uv add json6conda
conda install -c conda-forge json61M+
Monthly downloads
Python 3.8+
Required version
MIT
License
Basic Usage
The json6 library provides an API nearly identical to Python's built-in json module.
Parsing JSON6 Strings
import json6 # Parse JSON6 string with comments and trailing commas config_string = """ { // Application settings name: 'my-app', version: '1.0.0', features: [ 'comments', 'trailing commas', ], } """ config = json6.loads(config_string) print(config['name']) # 'my-app' print(config['features']) # ['comments', 'trailing commas']Converting Python to JSON6
import json6 data = { 'name': 'my-app', 'debug': True, 'ports': [3000, 3001], } # Convert to JSON6 string json6_string = json6.dumps(data, indent=2) print(json6_string) # { # name: 'my-app', # debug: true, # ports: [ # 3000, # 3001, # ], # }Drop-in Replacement for json
# Replace this: import json # With this: import json6 as json # Your existing code works with JSON6 features! data = json.loads('{ name: "app", /* comment */ }')API Reference
json6.loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Deserialize a JSON6 string to a Python object. Parameters match the standard json.loads().
import json6 from decimal import Decimal # With custom float parser data = json6.loads( '{ price: 19.99 }', parse_float=Decimal ) print(type(data['price'])) # <class 'decimal.Decimal'>json6.load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Deserialize a JSON6 file-like object to a Python object.
import json6 with open('config.json6', 'r') as f: config = json6.load(f) print(config)json6.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
Serialize a Python object to a JSON6 formatted string.
import json6 data = {'name': 'test', 'value': 42} # Compact print(json6.dumps(data)) # {name:'test',value:42} # Pretty-printed print(json6.dumps(data, indent=2)) # { # name: 'test', # value: 42, # } # With sorted keys print(json6.dumps(data, sort_keys=True))json6.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
Serialize a Python object to a JSON6 file.
import json6 data = {'name': 'app', 'version': '1.0'} with open('output.json6', 'w') as f: json6.dump(data, f, indent=2)Reading JSON6 Files
Basic File Reading
import json6 def load_config(path: str) -> dict: """Load a JSON6 configuration file.""" with open(path, 'r', encoding='utf-8') as f: return json6.load(f) config = load_config('config.json6') print(config)With Error Handling
import json6 from pathlib import Path def load_config(path: str, defaults: dict = None) -> dict: """Load JSON6 config with defaults and error handling.""" config_path = Path(path) defaults = defaults or {} if not config_path.exists(): print(f"Config not found: {path}, using defaults") return defaults try: with open(config_path, 'r', encoding='utf-8') as f: config = json6.load(f) return {**defaults, **config} except json6.JSON6DecodeError as e: print(f"Error parsing {path}: {e}") raise config = load_config('config.json6', { 'debug': False, 'port': 3000, })With Pydantic Validation
import json6 from pydantic import BaseModel from typing import List, Optional class DatabaseConfig(BaseModel): host: str port: int database: str ssl: bool = False class AppConfig(BaseModel): name: str version: str debug: bool = False database: DatabaseConfig def load_validated_config(path: str) -> AppConfig: """Load and validate JSON6 config with Pydantic.""" with open(path, 'r') as f: data = json6.load(f) return AppConfig(**data) config = load_validated_config('config.json6') print(config.database.host) # Fully typed!Command Line Usage
the json6 package includes a command-line tool for converting and validating JSON6 files.
Convert JSON6 to JSON
# Convert JSON6 to JSON python -m json6 config.json6 > config.json # Or with pipe cat config.json6 | python -m json6Validate JSON6
# Check if file is valid JSON6 python -m json6 --check config.json6 && echo "Valid JSON6"Alternative Libraries
While json6 is the most popular choice, here are other Python JSON6 libraries:
pyjson6
A fast JSON6 parser written in Cython for better performance.
pip install pyjson6 import pyjson6 data = pyjson6.loads('{ key: "value" }')commentjson
Simpler library that adds comment support to JSON (but not all JSON6 features).
pip install commentjson import commentjson data = commentjson.loads('{ "key": "value" /* comment */ }') Recommendation: Use the standard json6 package for full JSON6 compliance. Use pyjson6 if you need maximum parsing speed.
Common Patterns
Environment-Specific Config
import json6 import os from pathlib import Path def load_env_config() -> dict: """Load config based on environment.""" env = os.getenv('APP_ENV', 'development') config_dir = Path('config') # Load base config with open(config_dir / 'base.json6') as f: config = json6.load(f) # Override with environment-specific config env_config_path = config_dir / f'{env}.json6' if env_config_path.exists(): with open(env_config_path) as f: env_config = json6.load(f) config.update(env_config) return configType Hints with TypedDict
import json6 from typing import TypedDict, List class ServerConfig(TypedDict): host: str port: int workers: int class AppConfig(TypedDict): name: str version: str server: ServerConfig features: List[str] def load_config(path: str) -> AppConfig: with open(path) as f: return json6.load(f) # type: ignore config: AppConfig = load_config('config.json6') print(config['server']['port']) # Type checker knows this is intReady to Use JSON6 in Python?
Explore more examples or check out other language guides.