Question

I have a tree structure and i would like to create a JSON schema.

The class structure

class Node {

   String id;
   List<Node> children = new ArrayList<>();

}

The JSON Schema so far:

{
  "name": "node",
  "type": "object",
  "properties": {
     "id": {
        "type": "string",
        "description": "The node id",
        "required": true
     }
     "children": {
        "type": "array",
        "items": {
           //The items of array should be node ?               
        }
     }
  }
}

My problem is that I do not know how should i describe the content "items" of array in JSON?

Thanks in advance for answer.

Était-ce utile?

La solution

Just use a JSON reference to point back to the schema itself:

{
  "type": "object",
  "required": [ "id" ],
  "properties": {
     "id": {
        "type": "string",
        "description": "The node id"
     },
     "children": {
        "type": "array",
        "items": { "$ref": "#" }
     }
  }
}

The # JSON reference means in essence "the document itself". This therefore allows you to define recursive schemas, as here.

Note: rewritten so that it conforms to draft v4.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top