Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is a hash in regards to JSON?

Tags:

json

hashmap

hash

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.

like image 422
Alex Avatar asked Mar 02 '10 15:03

Alex


People also ask

What's the difference between a hash and a JSON?

Hash and Dictionary are what JSON Objects are called in other languages. 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.

Is a JSON object a hash table?

The Theory of JSONStandard objects are either a single key and value, or else a collection of keys and values which are equivalent to a hash table in most languages (learn about hash tables in Lua).

What is the purpose of hashes?

A hash table stores key and value pairs in a list that is accessible through its index. Because key and value pairs are unlimited, the hash function will map the keys to the table size. A hash value then becomes the index for a specific element.

What is a hash data type?

It is an abstract data type that maps keys to values. A hash table uses a hash function to compute an index, also called a hash code, into an array of buckets or slots, from which the desired value can be found. During lookup, the key is hashed and the resulting hash indicates where the corresponding value is stored.


1 Answers

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.

like image 134
Lior Cohen Avatar answered Sep 20 '22 08:09

Lior Cohen