Sequester Sequester - 3 months ago 17
JSON Question

Merge properties of one Json schema in another Json schema

I have a schema whose properties I have to merge in another schema. Suppose following is the schema whose properties have to be included:

{"$schema": "http://json-schema.org/schema#",
"title": "native",
"type": "object",
"required": ["executable"],
"properties":
{
"job_name":
{
"type":"string",
"dname": "job name"
},
"executable":
{
"type":"string",
"dname":"server",
"description": "server name"
}
}}


The schema that includes the above schema is of the form:

{"$schema": "http://json-schema.org/schema#",
"title": "application",
"type": "object",
"required": ["app_name"],
"properties":
{
"app_name":
{
"type":"string",
"dname": "app name"
}
}}


I want to merge the properties of first schema in the second one so that the second one looks like :

{"$schema": "http://json-schema.org/schema#",
"title": "native",
"type": "object",
"required": ["executable","app_name"],
"properties":
{
"job_name":
{
"type":"string",
"dname": "job name"
},
"executable":
{
"type":"string",
"dname":"server",
"description": "server name"
}
"app_name":
{
"type":"string",
"dname": "app name"
}
}}


Also, the "required" field gets added to the second schema.
Is there a way it is possible?

Answer

I could extend one schema in another using 'extends' keyword as follows :

  • Define the first schema in the same file in the definitions (I hope it can be another json schema file as well) and extend it in another schema.

    "definitions" : {
     "argumentBaseType":{
     "required": ["executable"],
     "properties":
     {
      "job_name":
      {
       "type":"string",
       "dname": "job name"
      },
     "executable":
     {
      "type":"string",
      "dname":"server",
      "description": "server name"
     }
    }
    }
    "properties":
    {
    {
     Argument1 : {
      "extends" : {
        "$ref" : "#/definitions/argumentBaseType"
      },
      "app_name":
      {
       "type":"string",
       "dname": "app name"
      }
     }}
    
Comments