从 JSON 模式生成 Python JSON 虚拟数据

标签 python json dummy-data

我正在寻找一个 python 库,我可以在其中输入我的 JSON 模式并生成虚拟数据。我在 javascript dummy-json 中使用过类似的库。有没有人知道可以在 python 中执行相同操作的库。

最佳答案

做这件事的库是 hypothesis-jsonschema

Hypothesis是一个可以生成符合给定规范的任意数据的库。

hypothesis-jsonschema使得将 JSON Schema 转换为可供 Hypothesis 使用的规范成为可能。

下面是一个使用 Hypothesis 和 hypothesis-jsonschema 编写的单元测试示例:

from hypothesis import given

from hypothesis_jsonschema import from_schema


@given(from_schema({"type": "integer", "minimum": 1, "exclusiveMaximum": 10}))
def test_integers(value):
    assert isinstance(value, int)
    assert 1 <= value < 10

关于从 JSON 模式生成 Python JSON 虚拟数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35231234/

相关文章:

python - 使用实际值创建虚拟列

R-生成一个 "missing values variable"

Python functools 部分效率

sql - 什么是 postgres 替代 lower() 之类的 %

python - 如何在 Python 中将变量添加到 JSON 列表?

javascript - 在javascript中解析复杂的json

python - matplotlib 动画线图保持为空

python - Pandas 获取元素直到值发生变化

Python 3.x 打印特定标题后的行数

java - 如何在没有任何底层数据库的情况下运行 SQL 表达式查询