本文へ移動
cccskills
無料GitHub で公開

python-json-parsing

Python JSON parsing best practices covering performance optimization (orjson/msgspec), handling large files (streaming/JSONL), security (injection prevention), and advanced querying (JSONPath/JMESPath).

インストール方法を見る

含まれるファイル(2)

  • SKILL.md5.9 KB
  • reference/python-json-parsing-best-practices-2025.md13.6 KB

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

Python JSON Parsing Best Practices

Comprehensive guide to JSON parsing in Python with focus on performance, security, and scalability.

Quick Start

Basic JSON Parsing

import json

# Parse JSON string
data = json.loads('{"name": "Alice", "age": 30}')

# Parse JSON file
with open("data.json", "r", encoding="utf-8") as f:
    data = json.load(f)

# Write JSON file
with open("output.json", "w", encoding="utf-8") as f:
    json.dump(data, f, indent=2)

Key Rule: Always specify encoding="utf-8" when reading/writing files.

When to Use This Skill

Use this skill when:

  • Working with JSON APIs or data interchange
  • Optimizing JSON performance in high-throughput applications
  • Handling large JSON files (> 100MB)
  • Securing applications against JSON injection
  • Extracting data from complex nested JSON structures

Performance: Choose the Right Library

Library Comparison (10,000 records benchmark)

LibrarySerialize (s)Deserialize (s)Best For
orjson0.421.27FastAPI, web APIs (3.9x faster)
msgspec0.490.93Maximum performance (1.7x faster deserialization)
json (stdlib)1.621.62Universal compatibility
ujson1.411.85Drop-in replacement (2x faster)

Recommendation:

  • Use orjson for FastAPI/web APIs (native support, fastest serialization)
  • Use msgspec for data pipelines (fastest overall, typed validation)
  • Use json when compatibility is critical

Installation

# High-performance libraries
pip install orjson msgspec ujson

# Advanced querying
pip install jsonpath-ng jmespath

# Streaming large files
pip install ijson

# Schema validation
pip install jsonschema

Large Files: Streaming Strategies

For files > 100MB, avoid loading into memory.

Strategy 1: JSONL (JSON Lines)

Convert large JSON arrays to line-delimited format:

# Stream process JSONL
with open("large.jsonl", "r") as infile, open("output.jsonl", "w") as outfile:
    for line in infile:
        obj = json.loads(line)
        obj["processed"] = True
        outfile.write(json.dumps(obj) + "\n")

Strategy 2: Streaming with ijson

import ijson

# Process large JSON without loading into memory
with open("huge.json", "rb") as f:
    for item in ijson.items(f, "products.item"):
        process(item)  # Handle one item at a time

See: patterns/streaming-large-json.md

Security: Prevent JSON Injection

Critical Rules:

  1. Always use json.loads(), never eval()
  2. Validate input with jsonschema
  3. Sanitize user input before serialization
  4. Escape special characters (" and \)

Vulnerable Code:

# NEVER DO THIS
username = request.GET['username']  # User input: admin", "role": "admin
json_string = f'{{"user":"{username}","role":"user"}}'
# Result: privilege escalation

Secure Code:

# Use json.dumps for serialization
data = {"user": username, "role": "user"}
json_string = json.dumps(data)  # Properly escaped

See: anti-patterns/security-json-injection.md, anti-patterns/eval-usage.md

Advanced: JSONPath for Complex Queries

Extract data from nested JSON without complex loops:

import jsonpath_ng as jp

data = {
    "products": [
        {"name": "Apple", "price": 12.88},
        {"name": "Peach", "price": 27.25}
    ]
}

# Filter by price
query = jp.parse("products[?price>20].name")
results = [match.value for match in query.find(data)]
# Output: ["Peach"]

Key Operators:

  • $ - Root selector
  • .. - Recursive descendant
  • * - Wildcard
  • [?<predicate>] - Filter (e.g., [?price > 20])
  • [start:end:step] - Array slicing

See: patterns/jsonpath-querying.md

Custom Objects: Serialization

Handle datetime, UUID, Decimal, and custom classes:

from datetime import datetime
import json

class CustomEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.isoformat()
        if isinstance(obj, set):
            return list(obj)
        return super().default(obj)

# Usage
data = {"timestamp": datetime.now(), "tags": {"python", "json"}}
json_str = json.dumps(data, cls=CustomEncoder)

See: patterns/custom-object-serialization.md

Performance Checklist

  • Use orjson/msgspec for high-throughput applications
  • Specify UTF-8 encoding when reading/writing files
  • Use streaming (ijson/JSONL) for files > 100MB
  • Minify JSON for production (separators=(',', ':'))
  • Pretty-print for development (indent=2)

Security Checklist

  • Never use eval() for JSON parsing
  • Validate input with jsonschema
  • Sanitize user input before serialization
  • Use json.dumps() to prevent injection
  • Escape special characters in user data

Reference Documentation

Performance:

  • reference/python-json-parsing-best-practices-2025.md - Comprehensive research with benchmarks

Patterns:

  • patterns/streaming-large-json.md - ijson and JSONL strategies
  • patterns/custom-object-serialization.md - Handle datetime, UUID, custom classes
  • patterns/jsonpath-querying.md - Advanced nested data extraction

Security:

  • anti-patterns/security-json-injection.md - Prevent injection attacks
  • anti-patterns/eval-usage.md - Why never to use eval()

Examples:

  • examples/high-performance-parsing.py - orjson and msgspec code
  • examples/large-file-streaming.py - Streaming with ijson
  • examples/secure-validation.py - jsonschema validation

Tools:

  • tools/json-performance-benchmark.py - Benchmark different libraries

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

同じリポジトリのスキル

概要と使いどころ

Primary accessibility skill for VS Code. REQUIRED for new feature and contribution work, and also applies to updates of existing UI. Covers accessibility help dialogs, accessible views, verbosity settings, signals, ARIA announcements, keyboard navigation, and ARIA labels/roles.

日本語の概要は準備中です。原文の説明を表示しています。

jcasnellie69/homelab-config22026年10月11日 更新

Use this skill when the user explicitly asks to map, document, or onboard into an existing codebase. Trigger for prompts like "map this codebase", "document this architecture", "onboard me to this repo", or "create codebase docs". Do not trigger for routine feature implementation, bug fixes, or narrow code edits unless the user asks for repository-level discovery.

日本語の概要は準備中です。原文の説明を表示しています。

jcasnellie69/homelab-config22026年10月11日 更新

Act on user feedback attached to the current session. Use when the user submits feedback on the session's changes via the Submit Feedback button.

日本語の概要は準備中です。原文の説明を表示しています。

jcasnellie69/homelab-config22026年10月11日 更新

Add educational comments to the file specified, or prompt asking for file to comment if one is not provided.

日本語の概要は準備中です。原文の説明を表示しています。

jcasnellie69/homelab-config22026年10月11日 更新

Use when adding, modifying, or reviewing VS Code configuration policies. Covers the full policy lifecycle from registration to export to platform-specific artifacts. Run on ANY change that adds a `policy:` field to a configuration property.

日本語の概要は準備中です。原文の説明を表示しています。

jcasnellie69/homelab-config22026年10月11日 更新

Patterns and techniques for adding governance, safety, and trust controls to AI agent systems. Use this skill when: - Building AI agents that call external tools (APIs, databases, file systems) - Implementing policy-based access controls for agent tool usage - Adding semantic intent classification to detect dangerous prompts - Creating trust scoring systems for multi-agent workflows - Building audit trails for agent actions and decisions - Enforcing rate limits, content filters, or tool restrictions on agents - Working with any agent framework (PydanticAI, CrewAI, OpenAI Agents, LangChain, AutoGen)

日本語の概要は準備中です。原文の説明を表示しています。

jcasnellie69/homelab-config22026年10月11日 更新

jcasnellie69 のスキルをすべて見る

このスキルの問題を報告する