简体   繁体   中英

merge two JSON schemas (extend a JSON schema)

I have a "parent" JSON schema that I would like to extend to get various "child" schemas. I'll have about 20 children so it's important for me to find something consistent here.

Here's the parent (base schema):

  {
    "definitions":{
      "selector":{
        "$id":        "#/definitions/selector",
        "type":       "object",
        "title":      "Selectors used to extract datas.",
        "required":   [],
        "properties":{
          "path":{
            "type":     "string",
            "title":    "Selector path",
            "default":  "",
            "examples": []
          },
          "attr":{
            "type":     "string",
            "title":    "HTML attribute",
            "default":  "",
            "examples": ["src"]
          },
          "regex":{
            "type":     "string",
            "title":    "Regex pattern",
            "default":  "",
            "examples": []
          },
          "multiple":{
            "type":     "boolean",
            "title":    "Multiple values",
            "default":  false,
            "readOnly": true
          }
        }
      }
    },
  "type": "object",
  "title": "Track importer schema",
  "properties":{
    "artist":{
      "$ref": "#/definitions/selector",
      "title": "Track artist"
    },
    "title":{
      "$ref": "#/definitions/selector",
      "title": "Track title"
    },
    "album":{
      "$ref": "#/definitions/selector",
      "title": "Track album"
    }
  }
}

Here's the child (based on the parent)

  {
    'properties':{
      'artist':{
        'properties':{
          'path':{
            'default':'.//artist',
            'readOnly':true
          }
        }
      },
      'title':{
        'properties':{
          'path':{
            'default':'.//title',
            'readOnly':true
          }
        }
      },
      'album':{
        'properties':{
          'path':{
            'default':'.//album',
            'readOnly':true
          }
        }
      },
      'image':{
        'properties':{
          'path':{
            'default':'.//albumart',
            'readOnly':true
          }
        }
      }
    }
  }

I would like to get a new schema that would be the sum of the parent and child schemas.

Actually, I've found a way to have this working in Ruby by deep merging the two JSON schemas objects.

But since I need to use the schemas in multiple languages (Ruby, JavaScript, etc.), I would like to know if there is a way to do this "natively" with the JSON schemas.

Is that possible and how ?

Thanks !

"allOf": [
  { "$ref": "schema1.json" },
  { "$ref": "schema2.json" }
]

There is also the $dynamicAnchor and $dynamicRef keywords, which let you augment particular definitions with new properties, but it isn't necessary in your case because all of your properties are at the top level of the object, so two $ref s will work fine.

By the way, your $id s are invalid: identifiers cannot start with or include a fragment (at least not in any specification version that permits using $ref alongside other keywords, ie draft2019-09 or later).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM