2
votes

I want to use JSON Schema and to allow new types (currently there are string number array object boolean and null)

for example i want to add a type for contact

is it even possible in JSON? any suggestions?

thanks

4

4 Answers

3
votes

If your goal is to define contact once and re-use it many times, you can create a definition for an object named contact and then reference it with a JSON Pointer any time you need it.

One example of that might look like this:

{
    "type": "object",
    "definitions": {
        "contact": {
            "properties": {
                "name": {
                "type": "string"
                 },
                 "phone": {
                    "type": "string"
                }
            }
        }
    },
    "properties": {
        "Person": {
            "properties": {
                "contact": {
                    "$ref" : "#/definitions/contact"
                },
                "birthday": {
                    "type": "string",
                    "format": "date-time"
                }
            }
        },
        "Company": {
            "properties": {
                "contact": {
                    "$ref" : "#/definitions/contact"
                },
                "inBusinessSince" : {
                    "type": "string",
                    "format": "date-time"
                }
            }
        }
    }
}

Now suppose you wanted to have more properties on the contact attribute of Company than you do on Person. You can use the allOf keyword, so that Company above would now look like this:

"Company": {
    "properties": {
        "contact": {
            "allOf": [
                {"$ref" : "#/definitions/contact"},
                {
                    "properties": {
                        "fax": {
                            "type": "string"
                    }
                }
            ],
           "inBusinessSince" : {
               "type": "string",
               "format": "date-time"
            }
        }
    }

Refer to the following links for more Info.

2
votes

yes we can add type for contact in JSON

Lets' say your program requires data for type of Contact in this format

{
contact: [
{contactid:1, contactname: "abc", address:"abcdfg"},
{contactid:2, contactname: "hjk", address:"hjkdfg"}
]
}

Here Contact object has an id, name and address. we can create a schema for contact

{
"type" : "object",
"properties": {
         "contact":{
              "type":"object"
              "items":{
                  "type": "object",
                  "properties":{
                          "contactid": {"type":"number"}
                          "contactname": {"type":"string"}
                          "address": {"type":"string"}
                               }
                      }
                   }
                }
} 
0
votes

A type for "contact" would be of type object, wouldn't it? And this object then has several attributes, for example a string containing the adress. So there is simply no need to define new types, as every new type - if you break it down - consists of the primitive types string, number, array or object.

So for a contact you could write:

"contact"{
    "id": "contact",
    "required": false,
    "type": "object",
    "properties":{
        "address"{
            "id": "address",
            "required": true,
            "type": "string"
        }
        "phoneNumbers"{
            "id": "phoneNumbers",
            "required": false,
            "type": "array",
            "items":{
                "id": "0",
                "required": false,
                "type": "object",
                "properties":{
                    "type"{
                        "id": "type",
                        "required": false,
                        "type": "string"
                    }
                    "number"{
                        "id": "number",
                        "required": false,
                        "type": "string"
                    }
                }
            }
        }
    }
}

According to this schema a simple contact-object may look like this:

{
    "address": "Example Street 123",
    "phoneNumbers":
    [
        {
            "type": "home",
            "number": "123-456789"
        }
    ]
} 

Now say you have several contacts within another object, then you would have to add the contact schema in every place you store contact.
As mentioned above, I don't see any need to introduce new types, though you're schema can become very very big. Apart from this I don't know of any JSON schema specifications allowing new primitive types.

0
votes

There's a json schema for that: http://json-schema.org/card

To include it in your json schema:

{
    "contact": { "$ref": "http://json-schema.org/card"}
}