23

Despite much googling and hair-pulling, I can't for the life of me articulate the difference between json, objects, and arrays (in javascript). Below is how I've been using 2-dimensional data containers (afraid to use the words "array," "object," or "json" here). Please tell me what these two examples are?

//first example:
[
    {"record_id":1,"name":"Frank"},
    {"record_id":2,"name":"Sally"}
]

//second example:
{
"countries": 
    [
    {"id":1,"name":"Canada"},
    {"id":2,"name":"Mexico"}
    ],
"states":
    [
    "id":1,"name":"Maine"},
    {"id":2,"name":"Alaska"}
    ]
}
4
  • JSON is UTF-8 encoded text data laid out in a sequence of characters that conforms to the requirements of JSON's specification. How it's created makes no difference. The important thing is that you end up with UTF-8 data. Oct 6, 2012 at 22:15
  • @user1689607 Nit: JSON does not specify a UTF encoding (but it does mandate Unicode) - UTF-X is just how Unicode is mapped onto octets and this encoding happens independently of JSON. Otherwise, yes: JSON is a serialization format.
    – user166390
    Oct 6, 2012 at 22:22
  • The default encoding :-) However, a server may freely send back JSON as UTF-16 (or even UCS-2 .. maybe?) with the appropriate negotiation/headers.
    – user166390
    Oct 6, 2012 at 22:28
  • @pst: Yes, I was too specific about the encoding. The main point is that irrespective of the syntax, it is textual data. I think people become confused between syntax and data. But good points. :) Oct 6, 2012 at 22:28

5 Answers 5

63

JSON is a representation of the data structure, it's not an object or an array.

[1,2,3]

is an array.

{"foo":"bar"}

is an object.

In your example,

[
  {"record_id":1,"name":"Frank"},
  {"record_id":2,"name":"Sally"}
]

Is an array of objects.

{
  "countries": 
    [
      {"id":1,"name":"Canada"},
      {"id":2,"name":"Mexico"}
    ],
  "states":
    [
      {"id":1,"name":"Maine"},
      {"id":2,"name":"Alaska"}
    ]
}

Is an object containing other arrays and objects inside of it.

1
  • Thank you, Gary. This is the clearest explanation I've encountered so far. Appreciate it.
    – Octavient
    Oct 6, 2012 at 22:08
10

JSON is JavaScript Object Notation. This is simply a way of writing down JavaScript data types. It is not a data type in-and-of-itself.

See below for some examples of JavaScript data types, and the literal notation of creating them.

JSON can be used to send data from the server to the browser, for example, because it is easy for JavaScript to parse into a normal JavaScript data structure.

In your example, you are using lists of objects, and objects of objects.

This is a list of 3 empty objects.

[{}, {}, {}]

This is a list of three simple records:

var mylist = [
    {name: 'John', age: 24},
    {name: 'Bill', age: 42},
    {name: 'Jill', age: 18},
    ]

You can access it like this:

mylist[1].name
>>> 'Bill'

mylist[2].age
>>> 18

JavaScript has several data types:

Number

1
100
-2000
123.45

String

"Hi John"
"Message:\nGo Forth"

Boolean

true
false

Array

[1,2,3]
[]
["a", "b", 123]
["a", "b", 123, [3,4,5]]

Object

{}
{a: 10}
{mylist: [1,2,3], yourlist: [4,5,6]}
{myself: {name: 'me', age: 10}, yourself: {name: 'you', age: 20}}
6

You use { braces } to declare an object literal.

You use [ square brackets ] to declare an array literal.

Objects are collections of key name value pairs.

Here's an example of an array of strings:

var a = [ "one", "two", "three" ];

Here's an example of a simple object that represents a person:

var personObject = {
    name: 'Joe',
    age: 25,
    hometown: 'New York'        
};
1

JSON is a textual data interchange format. As its name ("JavaScript Object Notation") suggests, it originates from JS; meaning that JSON is actually syntactically valid JavaScript. In other words, you can paste a JSON string directly into your JS code.

Arrays are special Objects. They can be constructed by [].

Objects can be constructed via {}.

So what you have in your example is two JSON strings, one representing an Array of objects, the second one representing an Object whose properties are themselves Arrays of Objects.

0

Well i believe objects can have methods and properties while arrays cant. JSON can be passed to the server while array cant be, unless you pass it as a string by POST

1
  • This answer may be confusing. JSON can encode arrays which can be passed to the server, just as JSON can encode objects which can be passed to the server. Also, JSON objects cannot have methods, as JSON is not a scripting or programming language (it is a data interchange format).
    – CristosLC
    Apr 10, 2017 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.