JSON#
JavaScript Object Notation
Why use JSON Schema#
- Describe existing data format
- Define rules and constraints
- Clear and readable documentation
- Highly extensible
- Vaildate your data
- Automate testing
- Enhance data quality
- Wide range of tools availabiility
JSON Data Type#
-
Arrays
string, number, object, array, Boolean, or null
[ "first", 2, true, null ]
-
Boolean
true, false. Doesn't need
"
true false
-
Null
When no value is assigned to a key, it can be treated as null.
null
-
Number
Both positive and negative numbers as well as decimal points. A JSON numbers follows JavaScript's double-precision floating-point format (1).
-
- 64位元 IEEE 754浮點數
- 範圍: -21024 ~ +21023
465.3213
-
-
Ojbect
{ key1: value1, key2: value2 }
-
String
Strings are enclosed in
"
, can contain any Unicode (1) character.-
- 因為ASCII code不足以支持所有的語系,所以有了Unicode的誕生。
- UTF-8:
- Unicode Transformation Format (UTF)
- 可變長度的編碼,使用1到4個位元組來表達一個字元,並與ASCII相容
- 世界最廣泛的編碼方式
"This is a string"
-
JSON Schema#
- Data is name(key)/ value pairs
- Data is separated by commas
- Curly braces hold objects
- Square brackets hold arrays
Keywords#
Metadata keywords#
$schema
: states that this schema complies$id
: provides a base URI for referencing this schema, type, or propertytitle
: describes the intent of a schemadescription
: provides a definition for this type or property
Validation Keywords#
-
Numbers and Integers
multipleOf
: limit the value to be a multiple of a given numberminimum
,exclusiveMinimum
,maximum
,exclusiveMaxmum
: restrict the range of values
-
Strings
minLength
,maxLength
: limit the length of the stringpattern
: require the value to satisfy of the stringformat
: require the value to match a certain format
-
Arrays
type
: limit the type of each itemenum
: define the alloable values of each item with an arrayminItems
,maxItems
: limit the number or itemsuniqueItems
: require each item to be unique
-
Objects
properties
: list properties that may be included in the objectrequired
: list properties that must be included in the objectadditionalProperties
: allow properties that are not listed in the type definitionminProperties
,maxProperties
: limit the number of properties in the objectpatternProperties
: define types for properties based on their namedependencies
: add restrictions if certain conditions are met
allOf
: must be valid against all of the subschemasanyOf
: must be valid against any of the subschemas-
oneOf
: must be valid against exactly one of the subschemas -
$ref
,$def
: must be an object, and object must be a valid JSON schema
Create a Simple JSON Schema#
Differences#
JSON vs. XML vs. YAML
JSON#
- Commonly used for data storage and transfer.
XML (Extensible Markup Language)#
- It is general-purpose markup language similar to JSON that allows for more complex data structures.
YAML#
-
It was designed specifically to be easier for humans to read, and originally built to simplify XML.
- Indentation to represent objects
- Colons to separate key-value pairs
- Hyphens for arrays
- Hashes to denote a comment