如何使用现有的json架构来获取属性?

时间:2015-03-30 11:04:55

标签: json jsonschema

我的产品json是:

{
   "id": 123,
   "variant":{
      "id":123,
      "name":"variant 1" 
    }
}

我有 Variant 对象的json架构,我需要为 Product 对象创建架构。如何在产品架构中使用现有的变体架构?

实施例

产品(未优化)架构:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://example.com/scheme/product",
  "type": "object",
  "title": "Product",
  "description": "An explanation about the puropose of this instance described by this schema.",
  "name": "Product",
  "properties": {
    "id": {
      "id": "http://example.com/scheme/product/id",
      "type": "integer",
      "title": "Id schema.",
      "description": "An explanation about the puropose of this instance described by this schema.",
      "name": "id"
    },
    "variant": {
      "id": "http://example.com/scheme/product/variant",
      "type": "object",
      "title": "Variant schema.",
      "description": "An explanation about the puropose of this instance described by this schema.",
      "name": "variant",
      "properties": {
        "id": {
          "id": "http://example.com/scheme/product/variant/id",
          "type": "integer",
          "title": "Id schema.",
          "description": "An explanation about the puropose of this instance described by this schema.",
          "name": "id"
        },
        "name": {
          "id": "http://example.com/scheme/product/variant/name",
          "type": "string",
          "title": "Name schema.",
          "description": "An explanation about the puropose of this instance described by this schema.",
          "name": "name"
        }
      }
    }
  },
  "required": [
    "id",
    "variant"
  ]
}

变体架构:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://example.com/scheme/variant",
  "type": "object",
  "title": "Variant",
  "description": "An explanation about the puropose of this instance described by this schema.",
  "name": "Variant",
  "properties": {
    "id": {
      "id": "http://example.com/scheme/variant/id",
      "type": "integer",
      "title": "Id schema.",
      "description": "An explanation about the puropose of this instance described by this schema.",
      "name": "id"
    },
    "name": {
      "id": "http://example.com/scheme/variant/name",
      "type": "string",
      "title": "Name schema.",
      "description": "An explanation about the puropose of this instance described by this schema.",
      "name": "name"
    }
  },
  "required": [
    "id",
    "name"
  ]
}

2 个答案:

答案 0 :(得分:1)

$ref救援。

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://example.com/scheme/product",
  "type": "object",
  "title": "Product",
  "description": "An explanation about the puropose of this instance described by this schema.",
  "name": "Product",
  "properties": {
    "id": {
      "id": "http://example.com/scheme/product/id",
      "type": "integer",
      "title": "Id schema.",
      "description": "An explanation about the puropose of this instance described by this schema.",
      "name": "id"
    },
    "variant": {
      "$ref": "http://example.com/scheme/product/variant"
    }
  },
  "required": [
    "id",
    "variant"
  ]
}

答案 1 :(得分:0)

只需使用$ref即可。假设您的变体模式是在同一文件中的节点定义下定义的,它将是这样的:

"variant": {"$ref": "#/definitions/variant"}