[LegAL AI] Part 2-1-2: A Quick Guide to Using Open Law Korea

-Daniel

Recently, I had the chance to dive into the Open Law Korea API to extract specific legal data I needed for a project. Admittedly, I’ve been swamped lately, so my update on this is a little delayed, but here it is! This post is about how I streamlined fetching and processing legal information using their API.

Exploring the API Structure

The Open Law API provides a versatile interface for retrieving Korean legal data, including statutes, precedents, and administrative rulings. Most of the data comes in JSON format, which is convenient for modern applications. However, in some cases, only XML or HTML outputs are available, requiring additional parsing.

I’ve included some visuals to illustrate the journey:

  • Response Structures: Screenshots of fetched JSON responses, showing page structures and keys like 법령ID, 법령일련번호, and more.
  • API Endpoints: A mind map summarizing the available API calls.
  • Detailed Documentation: The official API parameter reference table for understanding how to construct queries.

The Code in Action

Here’s a snippet of the Python script I used to fetch law details. It’s straightforward, leveraging Python’s requests library for HTTP interactions and parsing data:

import json
import requests

def load_law_ids(filename):
    """Load law IDs from the search results JSON file."""
    with open(filename, 'r', encoding='utf-8') as f:
        laws = json.load(f)
    return [(law['법령ID'], law['법령일련번호']) for law in laws]

def fetch_law_detail(mst, law_id):
    """Fetch detail for a single law."""
    base_url = "http://www.law.go.kr/DRF/lawService.do"
    params = {
        "OC": "test",
        "target": "law",
        "type": "JSON",
        "MST": mst
    }
    headers = {
        "Accept": "application/json",
        "Accept-Language": "ko-KR,ko;q=0.9",
        "Cache-Control": "no-cache",
        "User-Agent": "Mozilla/5.0 ..."
    }
    response = requests.get(base_url, params=params, headers=headers)
    return response.json()

You can see in the screenshots how the API fetches data in batches (e.g., 100 precedents per page). This segmentation made it easy to navigate through a large dataset.


Visual Demonstration

1. Fetching Law IDs

  • I started by retrieving a list of IDs using a search query.
  • Each ID represents a unique law document, which I later used to fetch detailed information.

2. Detailed View with JSON/XML

  • The API allows fetching detailed law texts, which can be output as JSON, XML, or rendered HTML. The images below showcase both XML and HTML views of a sample law (건강가정기본법).

3. Mind Map of API Endpoints

  • To better understand the scope of the API, I created a visual map of endpoints and their purposes, making navigation through documentation a breeze.

Challenges & Learnings

  • Format Variations: Not all data was available in JSON, requiring flexibility in handling XML and HTML.
  • Batch Processing: With over 800 pages of data, managing efficient pagination was critical.

Wrapping Up

The Open Law Korea API is an excellent tool for legal tech enthusiasts, developers, and researchers. With just a few tweaks, you can automate data collection for analysis, legal AI training, or personal research.

For those curious about the full demonstration, I’ve also included a short screen-recorded video showcasing how I ran this script to fetch data dynamically.

Happy coding!


Feel free to ask questions or share your experiences with similar projects. Let’s build together!

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다