Altova XMLSpy 2024 Professional Edition

Validate JSON Documents

Home Prev Top Next

XMLSpy contains a JSON validation engine that can be invoked to do the following:

 

If a JSON schema is the active document: Validates the JSON schema against the appropriate JSON Schema specification (for which no additional schema assignment is needed); the schema version is indicated by the $schema keyword; the validation can be carried out in any of the three views (Text, Grid, and JSON Schema).

If a JSON instance is the active document: Validates the JSON instance against a JSON schema. The schema is assigned to the JSON instance as described below. JSON instance validation can be carried out in Text View and Grid View.

If a JSON5 instance is the active document: Validates the JSON instance against a JSON schema. The schema is assigned to the JSON5 instance as described below. JSON5 instance validation can be carried out in Text View and Grid View.

 

Avro validation (Enterprise Edition only)

Avro data and Avro schema documents, as JSON documents, can be validated in Text View and Grid View:

 

If an Avro data instance in JSON format is the active document: Validates the Avro instance against an Avro schema. The schema is assigned to the instance as described below.

If an Avro schema is the active document: Validates the Avro schema against the Avro schema specification (no schema assignment is needed); the validation can be carried out in Text View or Grid View.

 

Assigning a JSON schema to a JSON instance

JSON instance documents can be validated against a JSON schema.

 

To set the JSON schema against which you want to validate a JSON or YAML document, do the following:

 

1.Make the JSON/YAML document the active document.

2.In the JSON tab of the Info window (screenshot below), click the arrow icon next to Validation against schema, and, in the menu that appears, click Select JSON Schema. (Note that Avro support is available in XMLSpy Enterprise Edition only.)

JSONInfoWindow

Note that the JSON schema assignment is not written into the JSON or YAML document, but entered in the Info window of XMLSpy. When you now validate the JSON or YAML document, the JSON schema file in the Info window will be the file used for validation.

 

To remove the assignment, select the command Remove Schema from the same menu (see screenshot above).

 

Note:If the JSON or YAML file is part of an XMLSpy project, then the JSON schema for validation can also be assigned via the Project Properties dialog (use the Validate With option in this dialog). If you then validate a project folder, all the JSON and YAML files in the project folder will be validated against the JSON schema. If you wish to run JSON and YAML validation separately, then we recommend that you put each document type in a separate project subfolder.

 

For information about generating a JSON schema from the JSON instance, see the section Generating JSON Schema from a JSON Instance.

 

Validating instance and schema documents

Select the command XML | Validate XML (F8) or click the Validate (F8) icon ic_validate in the toolbar to validate the active JSON document (instance or schema). If an instance document is being validated, a schema document must be assigned to the instance (see above). Validation results are displayed in the Messages window. Errors are also flagged in the line-numbering margin. If a smart fix is available for an error, then a light bulb icon is shown on the line that generates the error. When you place the mouse over the icon, a popup appears that lists available smart fixes. Select a fix to apply it immediately.

Note:The validation error indicators and smart fixes described above are refreshed only when the XML | Validate (F8) command is executed; they are not updated in the background. So, after correcting an error, you must run the Validate (F8) command again to make sure that the error has indeed been fixed.

 

To go to the schema document from the instance document, double-click the schema in the Info window (see screenshot above), or select the command DTD/Schema | Go to Schema. To go directly to the schema definition of a JSON keyword or object, select the keyword or object in the instance document and select DTD/Schema | Go to Definition.

 

You can also validate a project folder containing JSON files by using the Validate command.

 

Validate on modification

The Validate on Edit mode is toggled on by default. When toggled on, well-formed checks and validation checks are carried out as you modify a document in JSON Grid View. For validation of a JSON document to be carried out (additional to well-formed checks), a JSON Schema must be assigned to the JSON document. Errors are shown by displaying erroneous text in red and flagging the location with a red exclamation mark. See Validating JSON Documents for more information.

 

The Validate on Edit mode can be toggled on/off either (i) via the XML | Validate on Edit menu command, (ii) the Validate on Edit toolbar button, or (iii) via the On Edit option of the Validation settings of the Options dialog.

 

© 2018-2024 Altova GmbH