3

I try to write a json schema for my data. The data looks that way:

{ "gold": [ { "id": "goldOne", "name": "firstGold", "title": "Gold 1 earned" }, { "id": "goldTwo", "name": "secondGold", "title": "Gold 2 earned" } ], "silver": [ { "id": "silberOne", "name": "firstSilver", "title": "Silver!" } ], "bronze": [ { "id": "bronzeOne", "name": "firstBronze", "title": "Bronze!" } ] } 

I already created the schema for the "gold"-array:

{ "$schema": "http://json-schema.org/draft-04/schema#", "title" : "trophy descriptions", "type": "object", "properties": { gold: { "description": "gold trophies", "type":"array", "items": { "type": "object", "properties": { "id": { "type": "string", "description": "unique identifier" }, "name": { "type": "string", "description": "label of trophy" }, "title": { "type": "string", "description": "description of trophy" } } } } } } 

Because the "silver" and "bronze" -arrays contain elements of the exact same type as "gold" I wonder if I have to write down the same stuff three times or if I can refer to a single description?

1 Answer 1

7

Yes, you can define and reference schemas through $ref keyword:

{ "$schema" : "http://json-schema.org/draft-04/schema#", "title" : "trophy descriptions", "type" : "object", "properties" : { "gold" : { "$ref" : "#/definitions/medal" }, "silver" : { "$ref" : "#/definitions/medal" }, "bronze" : { "$ref" : "#/definitions/medal" } }, "definitions" : { "medal" : { "type" : "array" // and whatever you want here } } } 
Sign up to request clarification or add additional context in comments.

2 Comments

If I use an array of certain objects, then a separate object is getting created for each element..... Can this be avoided?
Please add a new question for that new problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.