Altova XMLSpy 2025 Enterprise Edition

Generate JSON Schema from JSON Instance

Home Prev Top Next

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 it saves you the trouble of manually creating a schema from scratch. You can then modify or extend the generated schema according to your requirements.

 

Generate the JSON schema

You can generate a JSON schema from a JSON instance in one of these ways:

 

DTD/Schema menu: Make the JSON instance document the active document. Select the menu command DTD/Schema | Generate DTD/Schema.

JSON Info window: Make the JSON instance document the active document. In the JSON tab of the Info window (screenshot below), click the Arrow icon next to Validaton against schema and, in the dropdown menu that appears, select Generate JSON Schema.

JSONInfoWindow

In both cases, the Generate JSON Schema dialog appears (screenshot below in next section). Do the following:

 

1.Modify the settings as you want (see below for details) and click OK when done.

2.You will be prompted to provide a path and filename for the generated JSON schema. Enter these.

3.On clicking Save, the JSON schema will be generated and becomes the active document.

 

In the JSON instance document, the generated schema file will be assigned as the schema to use for validation (see the Info window; screenshot above); any previous assignment will be overwritten. To change the assignment, use the Select JSON Schema command of the JSON Info window's dropdown menu (screenshot above). For more information about validating JSON instances, see Validate JSON Documents.

 

Settings for JSON schema generation

You can specify options for JSON 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 unselected, 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.

 

© 2018-2024 Altova GmbH