I've seen this other question but it's not quite the same, and I feel like my issue is simpler, but just isn't working.

我的数据如下所示:

[
    { "loc": "a value 1", "toll" : null, "message" : "message is sometimes null"},
    { "loc": "a value 2", "toll" : "toll is sometimes null", "message" : null}
]

我想在一个 node 中使用AJV进行JSON验证.js项目,我try 了几种模式来描述我的数据,但我总是得到这样的错误:

[ { keyword: 'type',
    dataPath: '',
    schemaPath: '#/type',
    params: { type: 'array' },
    message: 'should be array' } ]

The schema I've tried looks like this:

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "loc": {
        "type": "string"
      },
      "toll": {
        "type": "string"
      },
      "message": {
        "type": "string"
      }
    },
    "required": [
      "loc"
    ]
  }
}

我还try 使用this online tool生成模式,但这也不起作用,为了验证它是否应该输出正确的结果,我try 使用jsonschemavalidator.net验证该输出,但这也给了我一个类似的错误:

Found 1 error(s)
 Message:
 Invalid type. Expected Array but got Object.
 Schema path:
 #/type

推荐答案

You have defined your schema correctly, except that it doesn't match the data you say you are validating. If you change the property names to match the schema, you still have one issue. If you want to allow "toll" and "message" to be null, you can do the following.

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "loc": {
        "type": "string"
      },
      "toll": {
        "type": ["string", "null"]
      },
      "message": {
        "type": ["string", "null"]
      }
    },
    "required": [
      "loc"
    ]
  }
}

However, that isn't related to the error message you are getting. That message means that data you are validating is not an array. The example data you posted should not result in this error. Are you running the validator on some data other than what is posted in the question?

Json相关问答推荐

json输入(嵌套数组)和输出需要jolt规范

JOLT规范:转移到现有数组

如何循环访问多个子数组并在单个JSON中检索数据点

如何将加权边列表导出到JSON树?

解析JSON说函数parse_json不存在?

JOLT转换以根据条件删除json对象

交换键和数组值,将旧键转换为新数组值,使用 jq

使用Powershell将JSON文件加载到SQL Server表格

带有 API 测试的 Typescript JSON 模式验证

无法在 json --data 中使用变量

如何在 Flutter 中遍历嵌套的动态 JSON 文件

从 JSON 响应中获取最新版本发布字段

在 Flutter 中将对象转换为可编码对象失败

如何判断字符串是否为json格式

JSON extract\set 的 SQLite JSON1 示例

.NET 对象最灵活的序列化是什么,但实现起来很简单?

如何使用 Swift 从 NSURLSession 获取 cookie?

android - 在 adb logcat 输出中格式化 json 字符串

没有默认构造函数的杰克逊第 3 方类

将 javascript 对象或数组转换为 json 以获取 ajax 数据