Validate your JSON object

Automatically translated from English

We use more and more the JSON format. For example, to save settings in a file, to store software configuration, or to exchange client-data on the web with other servers.

How can you validate that the received data is in the expected format? How can you validate that all the necessary information is actually present? How can you do this validation without writing a tiresome method for each type of JSON format?

Simply by writing a JSON schema and use the JSON Validate new command!

Preamble
As you may already know, there is a difference between a “Well Formed” XML document and a “Valid” XML document. An XML document with correct syntax is called “Well Formed” and an XML document validated against a DTD is both “Well Formed” and “Valid”.
You can do the same with the JSON documents. You can write a JSON schema to describe your JSON document. To know the grammar and the syntax for JSON schema, I advice you to go on the official site: http://json-schema.org/

JSON Validate command

With the JSON Parse command, you can already check that the JSON document is “Well Formed“. Since 4D v16 R4, the JSON Validate command allows you to verify that the JSON document is “Valid” according to your JSON schema.

Example database

The new JSON Validate command returns an object. In this object, the “success” attribute is a boolean, its value is true when the document is valid and false when the document is invalid against the JSON schema passed in parameter. The “errors” attribute is an object array with all information to understand and find where the error is.

Code example

For example, we use the following varSchema JSON schema:

{
     "$schema": "http://json-schema.org/draft-04/schema#",
     "title": "Person",
     "type": "object",
     "properties": {
         "firstName": {
             "type": "string"
         },
         "lastName": {
             "type": "string"
         },
         "gender": {
             "description": "Gender is M or F",
             "enum": [ "M","F" ]
         },
         "birthday": {
             "type": "object",
             "properties": {
                 "day": {"type": "number"},
                 "month": {"type": "number"},
                 "year": {"type": "number"}
             },
             "required": ["day", "month", "year"]
         }
     },
     "required": ["firstName", "lastName"]
 }

We want to check if the following varJSON JSON document is valid:

{
    "firstName": "John",
    "lastName": "Doe",
    "gender": "U" 
}

So we execute the following code:

C_OBJECT($oJSON;$oSchema;$oResult)
$oJSON:=JSON Parse(varJSON;*) // New * optional parameter
$oSchema
:=JSON Parse(varSchema)
$oResult
:=JSON Validate($oJSON;$oSchema)

Note: when you validate a JSON file, you want to easily retrieve the error position in the original file. Thus, we have added an optional ” * ” parameter to the JSON Parse command. In this case, 4D adds the information on the line number and the offset value for each attribute.

Below is what is returned in the “$oResult” object by the JSON Validate command:

{
    "success": false,
    "errors": [
        {
            "jsonPath": "gender",
            "line": 4,
            "offset": 4,
            "code": 22,
            "msg": "Error while validating against 'enum' key. \"U\" does not match any enum element in the schema.",
            "schemaPaths": "properties.gender.enum"
        }
    ]
}

Note: The implementation in v16 R4 supports this version of JSON schema validation.

Vanessa Talbot

• Product Owner •

Vanessa Talbot joined 4D Program team in June, 2014. As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.

Since her arrival, she has worked to define key features in 4D. She has worked on most of preemptive multi-threading new features and also on a very complex subject: the new architecture for engined application. Vanessa has a degree from Telecom Saint-Etienne. She began her career at the Criminal Research Institute as a developer for the audiovisual department. She has also worked in media and medical fields as expert in technical support, production as well as documenting new features.