Generating JSON Schema from a JSON Instance

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  User Guide and Reference > JSON, JSON Schema >

Generating JSON Schema from a JSON Instance

XMLSpy can generate a JSON schema from a JSON instance document (including from JSON5 instances). This feature is very useful since it quickly provides you with a schema based on an already existing JSON instance, and saves you the trouble of manually creating a schema from scratch. You can then modify or extend the generated schema according to your requirements.

 

Generating the schema

To generate a JSON schema from a JSON instance, do the following:

 

1.Make the JSON instance document the active document.
2.In the JSON tab of the Info Window, click the arrow icon next to Validation against schema, and, in the menu that appears, click Generate JSON Schema (see screenshot below; Avro support is available in the Enterprise edition only). Alternatively, select the menu command DTD/Schema | Generate DTD/Schema.

JSONInfoWindow

3.In the Generate JSON Schema dialog that now appears (see next section below), modify the settings as you want (see below for details). Click OK when done.
4.You will be prompted to provide a path and filename for the generated JSON schema. On clicking Save, the schema is generated and becomes the active document. In the instance document, the generated schema file will be assigned as the schema to use for validation; any previous assignment will be overwritten. To change the assignment, use the Select JSON Schema command of the context menu (see screenshot above).

 

Note:The version of the JSON schema will be the default version selected in the JSON tab of the Options dialog.

 

Settings for generating the schema

You can specify options for schema generation in the Generate JSON Schema dialog (screenshot below). See the previous section for information about how to access this dialog.

JSONGenerateJSONSchemaDlg

Detect array tuples

An array tuple is the sequence of items in an array. For example, the following array has a tuple with three items: [ 1, 2, "abc" ]. For the validation of arrays, the schema can specify whether the order and datatype of array (tuple) items are to be considered or not. If the Detect Array Tuples option is checked (see screenshot above), then the order and datatype of items will be detected. Based on what is detected, a corresponding definition will be created in the schema. The options for this setting are as follows:

 

Number of tuple items: A minimum and maximum number of tuple items can be specified. If a tuple in the instance has an item-count within this range, then this array will detected and defined.
Simple types only: Only tuples that have simple-type items (the atomic types string, number, integer, boolean, and null) are to be considered for detection.
Identically named arrays: Only arrays that are defined as values of properties that have the same name are considered for detection. For example, in the following JSON data fragment, the arrays marked with red-shaded brackets are all values of properties named a1 (shaded in blue): {"object1": [ { "a1": [ 1, 2, "abc" ] },  { "a1": [ 3, 4, "def" ] }, { "a1": [ 5, 6, "ghi" ] } ] }.
Minimum number of arrays: A minimum number of arrays for enabling array detection can be specified.

 

 

Other settings

 

Ignore order of object property names: If selected, the order of an object's properties is checked and recreated as closely as possible. Otherwise, the order is not checked.
Try to match the string format: The schema can specify that string datatypes must have a particular format. If this option is selected, then XMLSpy will try to detect the string format and add a format definition for strings wherever possible.
Make simple arrays local: A simple array is one in which all items are of the same simple datatype. If selected, all simple arrays will be defined locally in the schema, instead of using global definitions that are referenced locally.
Make simple objects local: A simple object is one in which all property values are of the same simple datatype. If selected, all simple objects will be defined locally in the schema, instead of using global definitions that are referenced locally.

 

Note:After the JSON schema has been generated, you can make local definitions of individual objects and arrays global, and vice versa. For more information, see the section Global and Local Definitions.

 


© 2019 Altova GmbH