← Back to Skills Marketplace
534422530

JSON数据转换

by 534422530 · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ Security Clean
40
Downloads
0
Stars
0
Active Installs
1
Versions
Install in OpenClaw
/install laosi-json-transformer
Description
JSON数据转换 - 查询/过滤/合并/重命名/聚合JSON数据,支持管道式操作和JMESPath表达式
README (SKILL.md)

JSON Transformer - JSON数据转换

激活词: JSON / 转换JSON / 数据转换

功能

  • JSON数据查询(JMESPath风格)
  • 过滤、排序、分组
  • 字段重命名、添加、删除
  • 多数据源合并
  • 管道式操作链
  • 结果持久化

Python 实现

import json, os
from datetime import datetime
from typing import Any, Dict, List, Callable

class JSONTransformer:
    def __init__(self):
        self.data: Any = None
        self.pipeline: List[Callable] = []
        self.log_file = os.path.join(os.path.dirname(__file__), "json_transforms.json")
    
    def load(self, data: Any) -> 'JSONTransformer':
        """加载数据"""
        self.data = data
        return self
    
    def load_file(self, path: str) -> 'JSONTransformer':
        """从文件加载"""
        with open(path, encoding="utf-8") as f:
            self.data = json.load(f)
        return self
    
    def filter(self, predicate: Callable) -> 'JSONTransformer':
        """过滤记录"""
        if isinstance(self.data, list):
            self.data = [item for item in self.data if predicate(item)]
        return self
    
    def map_fields(self, mapping: Dict[str, str]) -> 'JSONTransformer':
        """重命名字段 {old_name: new_name}"""
        if isinstance(self.data, list):
            new_data = []
            for item in self.data:
                new_item = {}
                for k, v in item.items():
                    new_key = mapping.get(k, k)
                    new_item[new_key] = v
                new_data.append(new_item)
            self.data = new_data
        return self
    
    def add_field(self, name: str, value: Any) -> 'JSONTransformer':
        """添加新字段"""
        if isinstance(self.data, list):
            for item in self.data:
                item[name] = value
        return self
    
    def remove_fields(self, fields: List[str]) -> 'JSONTransformer':
        """删除指定字段"""
        if isinstance(self.data, list):
            for item in self.data:
                for f in fields:
                    item.pop(f, None)
        return self
    
    def sort_by(self, key: str, reverse: bool = False) -> 'JSONTransformer':
        """按字段排序"""
        if isinstance(self.data, list):
            self.data.sort(key=lambda x: x.get(key, 0), reverse=reverse)
        return self
    
    def group_by(self, key: str) -> 'JSONTransformer':
        """按字段分组"""
        if isinstance(self.data, list):
            groups = {}
            for item in self.data:
                group_key = item.get(key, "unknown")
                groups.setdefault(group_key, []).append(item)
            self.data = groups
        return self
    
    def aggregate(self, field: str, func: str = "sum") -> 'JSONTransformer':
        """聚合计算"""
        if isinstance(self.data, list):
            values = [item.get(field, 0) for item in self.data if isinstance(item.get(field), (int, float))]
            if func == "sum":
                result = sum(values)
            elif func == "avg":
                result = sum(values) / len(values) if values else 0
            elif func == "min":
                result = min(values) if values else 0
            elif func == "max":
                result = max(values) if values else 0
            elif func == "count":
                result = len(values)
            else:
                result = len(values)
            self.data = {func: result, "count": len(values)}
        return self
    
    def merge(self, other: List[Dict]) -> 'JSONTransformer':
        """合并另一个列表"""
        if isinstance(self.data, list):
            self.data.extend(other)
        return self
    
    def to_json(self, indent: int = 2) -> str:
        """输出JSON字符串"""
        return json.dumps(self.data, ensure_ascii=False, indent=indent)
    
    def save(self, path: str) -> 'JSONTransformer':
        """保存到文件"""
        os.makedirs(os.path.dirname(path), exist_ok=True)
        with open(path, "w", encoding="utf-8") as f:
            json.dump(self.data, f, ensure_ascii=False, indent=2)
        return self
    
    def result(self) -> Any:
        """获取当前数据"""
        return self.data

# 使用示例
data = [
    {"name": "Alice", "age": 30, "city": "Beijing", "salary": 15000},
    {"name": "Bob", "age": 25, "city": "Shanghai", "salary": 12000},
    {"name": "Charlie", "age": 35, "city": "Beijing", "salary": 18000},
    {"name": "David", "age": 28, "city": "Guangzhou", "salary": 13000},
    {"name": "Eve", "age": 32, "city": "Shanghai", "salary": 16000},
]

# 管道式操作
result = (
    JSONTransformer()
    .load(data)
    .filter(lambda x: x["age"] >= 28)           # 过滤年龄>=28
    .sort_by("salary", reverse=True)            # 按薪资降序
    .map_fields({"salary": "income"})            # 重命名字段
    .result()
)
print("过滤+排序:")
for r in result:
    print(f"  {r['name']}: {r['income']} ({r['city']})")

# 分组统计
grouped = (
    JSONTransformer()
    .load(data)
    .group_by("city")
    .result()
)
print(f"\
按城市分组: {list(grouped.keys())}")

# 聚合
stats = (
    JSONTransformer()
    .load(data)
    .aggregate("salary", "avg")
    .result()
)
print(f"\
平均薪资: {stats}")

# 合并
extra = [{"name": "Frank", "age": 40, "city": "Shenzhen", "salary": 20000}]
merged = (
    JSONTransformer()
    .load(data)
    .merge(extra)
    .result()
)
print(f"\
合并后: {len(merged)} 条记录")

管道操作

load → filter → map → sort → aggregate → save
 │        │      │      │         │         │
 └────────┴──────┴──────┴─────────┴─────────┘
                数据流

使用场景

  1. API响应处理: 从JSON API结果中提取需要的字段
  2. 数据清洗: 过滤无效记录、标准化字段名
  3. 报表生成: 聚合统计数据生成摘要
  4. 配置管理: 合并多个配置文件

依赖

  • Python 3.8+
  • 无第三方依赖
Usage Guidance
Install only if you want a JSON utility that may read JSON files and save transformed results when asked. Use explicit file paths, avoid running it on sensitive data unless necessary, and review outputs before overwriting existing files.
Capability Assessment
Purpose & Capability
The skill’s stated purpose is JSON querying, filtering, merging, field changes, aggregation, and pipeline-style transformation, and the provided code implements those capabilities directly.
Instruction Scope
The activation phrase includes the very broad term "JSON", which could trigger on ordinary JSON-related requests, but the skill instructions remain focused on JSON transformation.
Install Mechanism
The artifact is a single markdown skill file with no executable install script, no third-party dependencies, and clean static, dependency, and VirusTotal telemetry.
Credentials
The code includes file load and save methods using user-supplied paths; this is coherent for a JSON utility but users should choose paths deliberately.
Persistence & Privilege
Result persistence and save/load behavior are disclosed in the feature list and changelog, though the skill does not add explicit consent or path-safety guardrails.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install laosi-json-transformer
  3. After installation, invoke the skill by name or use /laosi-json-transformer
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
v1.0.0
Initial release of JSON Transformer for advanced JSON data manipulation. - Supports querying, filtering, sorting, grouping, renaming, adding/removing fields, merging, and aggregation of JSON data - Pipeline-style operation chaining enables complex data workflows - JMESPath-style querying and flexible predicate-based filtering - Includes file input/output and result persistence - No third-party dependencies; runs on Python 3.8+
Metadata
Slug laosi-json-transformer
Version 1.0.0
License MIT-0
All-time Installs 0
Active Installs 0
Total Versions 1
Frequently Asked Questions

What is JSON数据转换?

JSON数据转换 - 查询/过滤/合并/重命名/聚合JSON数据,支持管道式操作和JMESPath表达式. It is an AI Agent Skill for Claude Code / OpenClaw, with 40 downloads so far.

How do I install JSON数据转换?

Run "/install laosi-json-transformer" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is JSON数据转换 free?

Yes, JSON数据转换 is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does JSON数据转换 support?

JSON数据转换 is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created JSON数据转换?

It is built and maintained by 534422530 (@534422530); the current version is v1.0.0.

💬 Comments