You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将MySQL Workbench数据库Schema导出为JSON用于Mockaroo数据生成?

解决MySQL Workbench Schema导出为JSON适配Mockaroo的方法

我之前也碰到过一模一样的需求——Mockaroo确实不直接支持.mwb文件导入,不过有几种靠谱的方法能把你的数据库Schema转换成它兼容的JSON格式:

方法1:SQL转JSON(最易上手,适合非技术用户)

  1. 导出SQL建表语句:打开你的.mwb文件,点击菜单栏的File > Export > Forward Engineer SQL CREATE Script,跟着导出向导勾选需要的数据库对象(表、列、约束等),生成完整的SQL建表文件。
  2. 转换为Mockaroo兼容的JSON:
    • 可以用在线的SQL转Schema JSON工具(不用跳转外部网站,直接搜这类工具就行),或者手动整理核心字段:每个表要包含名称、列名、数据类型、是否可为空、默认值这些Mockaroo需要的信息,整理成类似{"name": "表名", "fields": [{"name": "列名", "type": "数据类型", ...}]}的结构。

方法2:用MySQL Workbench Python脚本导出(精准可控)

如果你懂点Python,能直接利用Workbench的内置API导出最贴合需求的JSON:

  1. 打开Workbench,点击Scripting > New Script Tab,选择Python作为脚本语言。
  2. 粘贴下面的脚本(可以根据你的实际需求调整字段):
import json
from wb import *

# 获取当前打开的数据库模型
model = grt.root.wb.documents[0].physicalModels[0]
schema_result = []

# 遍历所有表提取信息
for table in model.catalog.schemata[0].tables:
    table_detail = {
        "name": table.name,
        "fields": []
    }
    for column in table.columns:
        column_detail = {
            "name": column.name,
            "type": column.simpleType.name,
            "nullable": column.isNotNull == 0,
            "default": column.defaultValue if hasattr(column, 'defaultValue') else None
        }
        table_detail["fields"].append(column_detail)
    schema_result.append(table_detail)

# 导出JSON文件
with open('mockaroo_schema.json', 'w') as output_file:
    json.dump(schema_result, output_file, indent=2)
  1. 点击脚本窗口的运行按钮,就能在指定路径生成直接适配Mockaroo的JSON Schema文件。

方法3:XML中转再转JSON

  1. 在Workbench里点击File > Export > Export to XML,导出整个模型的结构文件。
  2. 用XML转JSON的工具(比如Python的xmltodict库,或者在线转换工具)把XML转换成JSON,再根据Mockaroo的格式调整字段即可。

不管用哪种方法,最后把生成的JSON上传到Mockaroo的导入页面,就能快速生成你需要的测试数据啦。

内容的提问来源于stack exchange,提问作者peterHasemann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:15:45