如何在JSON模式中使用任意属性名称

发布于 2025-02-10 04:24:48 字数 301 浏览 0 评论 0原文

我正在使用JSON数据,其中其中一部分是一个键值对集合,其中键是任意字符串,值是字符串数组。我正在尝试为此编写一个模式,但我不确定如何表示任意属性名称。因此,对于这样的对象:

{
  "K1": ["V1", "V2", "V3"],
  "K2": ["V4", "V5"],
  "K3": ["V6", "V7"]
}

编写具有以下要求的JSON模式的正确方法:

  • 必须至少有一个属性。
  • 属性可以为其名称具有任何
  • 字符串
  • 任意

I'm working with JSON data where one portion of it is a key-value pair collection, where the key is an arbitrary string and the value is an array of strings. I'm trying to write a schema for this, but I'm not sure how to represent arbitrary property names. So for an object like this:

{
  "K1": ["V1", "V2", "V3"],
  "K2": ["V4", "V5"],
  "K3": ["V6", "V7"]
}

What would be the right way to write a JSON schema with the following requirements:

  • There must be at least one property.
  • Properties can have any arbitrary string for their name
  • Every property's associated value must be an array of strings
  • Every string array must contain at least one string (ie. must not be empty)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪少女 2025-02-17 04:24:48

您可以用statersProperties来表达属性或属性集的限制模式;它的值是一个模式,它根据匹配的所有属性的值进行评估。因此,例如,要表达一组以大写字母开头的属性的规则,您可以执行“ pattern properties”:{“^[az]”:{...更多规则在这里... 。要根据完整的模式定义属性名称的规则,您可以使用propertyNames

为了表达所有属性值的架构,无论名称如何,您都可以使用fromproperties。这些规则将与已经与属性匹配的任何属性匹配statersproperties

您可以使用minproperties表达最小数量的属性。

您可以用项目在数组中的所有项目表示规则。 Minitems可用于表达最小允许数量的项目。

您的数据架构可以通过:

{
  "type": "object",
  "minProperties": 1,
  "additionalProperties": {
    "type": "array",
    "minItems": 1,
    "items": {
      "type": "string"
    }
  }
}

https schema.org/understanding-json-schema/reference/object.html
https://json-schema.org.org.org.org/underting-json-schema/-json-schema/参考/array.html

You can express a restricted pattern for a property or set of properties with patternProperties; its value is a schema, which is evaluated against the values of all properties that match. So for example, to express a set of rules used for properties that start with a capital letter, you can do "patternProperties": { "^[A-Z]": { ... more rules here .... To define the rules for a property name in terms of a full schema, you can use propertyNames.

To express a schema for the value of all properties, regardless of name, you can use additionalProperties. These rules will not match against any property already matched with a properties or patternProperties.

You can express the minimum number of properties with minProperties.

You can express the rules for all items in an array with items. And minItems can be used to express the minimum allowable number of items.

The schema for your data can be expressed by:

{
  "type": "object",
  "minProperties": 1,
  "additionalProperties": {
    "type": "array",
    "minItems": 1,
    "items": {
      "type": "string"
    }
  }
}

https://json-schema.org/understanding-json-schema/reference/object.html
https://json-schema.org/understanding-json-schema/reference/array.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文