31

I am learning JSON, but I found out that you can put what are called "hashes" into JSON as well? Where can I find out what a hash is? Or could you explain to me what a hash is? Also, what's a hashmap? I have experience in C++ and C#, and I am learning JS, Jquery, and JSON.

Alex
  • 64,178
  • 48
  • 151
  • 180

4 Answers4

32

A Hash is a sparse array that uses arbitrary strings/objects (depending on the implementation, this varies across programming languages) rather than plain integers as keys.

In Javascript, any Object is technically a hash (also referred to as a Dictionary, Associative-Array, etc).

Examples:

var myObj = {}; // Same as = new Object();
myObj['foo'] = 'bar';

var myArr = []; // Same as = new Array();
myArr[0] = 'foo';
myArr[1] = 'bar';
myArr['blah'] = 'baz'; // This will work, but is not recommended.

Now, since JSON is basically using JS constructs and some strict guidelines to define portable data, the equivalent to myObj above would be:

{ "foo" : "bar" };

Hope this helps.

ניר
  • 1,204
  • 1
  • 8
  • 28
Lior Cohen
  • 8,985
  • 2
  • 29
  • 27
  • A note about your last line: you can set properties on arrays as if they were objects--they *are* objects, and will act like objects in that case. – skeggse Mar 28 '13 at 21:16
  • In Javascript, an Object is not "technically a hash", because "hash" is not the technical term for a hash map or dictionary or associative array etc. – bradw2k Mar 18 '22 at 19:27
16

Hash = dictionary.

A hash:

{ "key1": "value1", "key2": "value2" }
Instance Hunter
  • 7,837
  • 5
  • 44
  • 56
5

JSON supports dictionary type elements. People may refer to these as hash tables, which are a type of data structure. Referring to JSON dictionaries as hash tables would be technically incorrect, however, as there is no particular data structure implementation associated with the JSON data itself.

A hash is a random looking number which is generated from a piece of data and always the same for the same input. For example if you download files from some websites they will provide a hash of the data so you can verify your download is not corrupted (which would change the hash). Another application of hashes is in a hash table (or hash map). This is a very fast associative data structure where the hashes are used to index into an array. std::unorderd_map in C++ is an example of this. You could store a hash in JSON as a string for example something like "AB34F553" and use this to verify data.

Community
  • 1
  • 1
Brice M. Dempsey
  • 1,985
  • 20
  • 16
0

On json.org, a JSON "object" is "a collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array."

Using the ambiguous term "hash" for a JSON object is confusing. "Hash" is indeed used in the wild as short-hand for: hash map, dictionary, key-value structure, etc. But it is also short-hand for: the hash value that is computed by a hash function.

bradw2k
  • 693
  • 7
  • 11