json schema多种形式_Json-schema简介和应用

Json?

了解json schema首先要知道什么是json?

json 是 JavaScript Object Notation 的缩写,它是一种简化的数据交换格式,是目前互联网服务间进行数据交换最常见的一种交换格式,具有简洁、可读性好等特点。

在json中常见的数据类型主要包括

object

{ "key1": "value1", "key2": "value2" }

array

[ "first", "second", "third" ]

number

42

3.1415926

string

"This is a string"

boolean

true

false

null

null

一个示例json格式比如:

{

"fruits": [ "apple", "orange", "pear" ],

"vegetables": [

{

"veggieName": "potato",

"veggieLike": true

},

{

"veggieName": "broccoli",

"veggieLike": false

}

]

}

何为Json Schema

如前文所述,json是目前应用非常广泛的数据狡猾格式。既然是用于数据交换的格式,那么就存在数据交换的是双方,如何约定或校验对方的数据格式符合要求,就成了服务交互需要解决的一个问题。所以Json Schema就是用来定义json数据约束的一个标准。根据这个约定模式,交换数据的双方可以理解json数据的要求和约束,也可以据此对数据进行验证,保证数据交换的正确性。

目前最新的Json-schema版本是draft 7,发布于2018-03-19。下面我们就以官网的一个实例来看看Json-schema是如何进行数据约束以及其应用

如下是一个schema实例:

{

"$schema": "http://json-schema.org/draft-07/schema#",

"$id": "http://example.com/product.schema.json",

"title": "Product",

"description": "A product from Acme's catalog",

"type": "object",

"properties": {

"productId": {

"description": "The unique identifier for a product",

"type": "integer"

},

"productName": {

"description": "Name of the product",

"type": "string"

},

"price": {

"description": "The price of the product",

"type": "number",

"exclusiveMinimum": 0

},

"tags": {

"description": "Tags for the product",

"type": "array",

"items": {

"type": "string"

},

"minItems": 1,

"uniqueItems": true

},

"dimensions": {

"type": "object",

"properties": {

"length": {

"type": "number"

},

"width": {

"type": "number"

},

"height": {

"type": "number"

}

},

"required": [ "length", "width", "height" ]

}

},

"required": [ "productId", "productName", "price" ]

}

分析说明:

"$schema": "http://json-schema.org/draft-07/schema#",

说明当前使用的schema版本,可以不包含

"$id": "http://example.com/product.schema.json",

当前schema的唯一id标识,一般指向一个自主域名。方便后续引用,可以不包含

"title": "Product",

当前schema的标题,简要描述信息,可不包含

"description": "A product from Acme's catalog",

详细描述信息,可不包含

"type": "object",

约束对象是object,也就是在 { } 中的数据

"properties": {

"productId": {

"description": "The unique identifier for a product",

"type": "integer"

},

object中具体属性的约束,description是描述信息,不产生具体约束。

type约束productid属性类型为整型

"productName": {

"description": "Name of the product",

"type": "string"

},

约束productName属性类型为字符型

"price": {

"description": "The price of the product",

"type": "number",

"exclusiveMinimum": 0

},

约束price属性类型为数字型,可以是整型或浮点型。

exclusiveMinimum约束该数字>0(不包含0)

"tags": {

"description": "Tags for the product",

"type": "array",

"items": {

"type": "string"

},

"minItems": 1,

"uniqueItems": true

},

约束tag属性是array数组。items是数组项约束,这里约束数组项均为字符型

minItems数组至少包含1项。

uniqueItems约束数组中每项不得重复

"dimensions": {

"type": "object",

"properties": {

"length": {

"type": "number"

},

"width": {

"type": "number"

},

"height": {

"type": "number"

}

},

"required": [ "length", "width", "height" ]

}

},

约束dimensions嵌套对象,其中length,width,height均为数字类型

且这三个字段在dimensions对象中必须包含

"required": [ "productId", "productName", "price" ]

当前数据对象必须包含productId,productName,price三个字段

以上是对Json-Schema的简要说明,详细介绍大家可以关注后续更新和相关实战课。


版权声明:本文为weixin_39626131原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。