JSON Schema difference between Array and Hash array

1,444 views
Skip to first unread message

Bloody Curse

unread,
Feb 16, 2015, 5:03:14 AM2/16/15
to json-...@googlegroups.com
Hello there. I have the following array:

  "items": {
           
"title": "Items",
           
"type": "array",
           
"items": {
               
"title": "Item ",
               
"type": "object",
           
}
       
}

Now, this is a normal Array right? How would a hash array look like? Or is that a hash array? If yes, how would a normal array look like?

Jason Desrosiers

unread,
Feb 17, 2015, 1:15:13 AM2/17/15
to json-...@googlegroups.com
Yes, your example would describe a 0-indexed array.  You can use an object to describe a hash.  The simplest version would be `{}`.  This works because the default `type` is `object` and the default `additionalProperties` is `true`.

{
 
"title": "Items",
 
"type": "object",
 
"additionalProperties": true
}

This defines an object with any properties that have any value.  Effectively a hash.  You can restrict what values are allowed by using a schema instead of a boolean for `additionalProperties`.

{
 
"title": "Items".
 
"type": "object",
 
"additionalProperties": {

   
"title": "Item",
   
"type": "object"
 
}
}

This says that the values of the hash must be objects.  Actually, this effectively describes a hash of hashes.  You can also restrict the values of the keys of the hash using `patternProperties`.

{
 
"title": "Items",
 
"type": "object",
 
"patternProperties": {
   
"[A-Z][A-Z]": {

     
"title": "Item",
     
"type": "object"
   
}
 
}
}

This would restrict keys of the hash to be two capital letters (like state abbreviations for example).  Values would be hashes.

Austin Wright

unread,
Feb 17, 2015, 12:39:48 PM2/17/15
to json-...@googlegroups.com
It might be helpful to get some vocabulary out of the way first...

The example you have posted, at least in JSON terms, is part of an object, a kind of unordered key-value map. It would normally be found inside curly braces: {"a":1, "b":2, "c":3}

There's also arrays, a kind of vector of ordered items. They're found inside square brackets: [1, 2, 3]

I would stay away from the term "hash", which is short for "hash table", a particular implementation of a key-value map. There's no guarantee that the implementation you're using is going to be a hash table, it's just as likely to be a btree or similar kind of map that has different performance characteristics for lookup and writing.

Austin.

Jason Desrosiers

unread,
Feb 17, 2015, 1:56:04 PM2/17/15
to json-...@googlegroups.com
Austin, this type of data structure has many names in different programming languages.  They are called Map (Java), Dictionary (Python), associative array (PHP), object (JavaScript), Hash (Ruby), and probably more.  I agree that it is unfortunate that the term hash is ambiguous, but if that is the name the language uses, that is the best term to use in that context.  I assumed he was working in a context where hash is the appropriate term and used that term to describe it's equivalent in the context of json-schema.  It is best to use the appropriate name for the context you are in even if the name is ambiguous and overloaded like hash or object.
Reply all
Reply to author
Forward
0 new messages