- [Instructor] The basic structure of both
JavaScript objects and JSON that stores key value pairs
starts and ends with a curly brace.
For JavaScript arrays and JSON
that stores an ordered list of values,
the beginning and ending characters are square brackets.
The array format has all the same rules as the object format
and objects are subject to a few additional rules.
So let's compare a JavaScript object
and its JSON representation.
In JavaScript, the key doesn't require
punctuation around it, but it's okay to specify it
in single quotes or in double quotes.
In JSON, the key must be surrounded in double quotes.
So in JavaScript, I don't need quotes at all on the left,
but in JSON I do.
In both, the key is followed by a colon.
A value in a JavaScript object can store
different types of data, including strings, numbers,
true false values, and even more complex nested structures
like arrays and other objects.
In JavaScript, a string value must be enclosed in quotes,
but those can be single or double quotes or even backticks
in an ES6 environment.
In JSON, strings must be enclosed in double quotes,
no room for personal style here.
In both JavaScript and JSON, you use a comma to separate
one key value pair and the next.
Numerical values don't need quotes around them
in either JavaScript or JSON,
but JSON has an additional rule for numbers,
they can't start with zero.
If this truly is a numerical value
and the zero doesn't matter, I can simply represent it
in JSON as a number with no leading zero.
But if the zero is significant,
I might need to represent this as a string instead
enclosing it in quotes and relying on the system
receiving this data to work with it in that format.
Likewise, a numerical value in JavaScript
can end with a decimal point,
but in JSON, this is not allowed.
Usually this just means that you need to ensure
the decimal is removed.
Boolean values, which have a value of either true or false,
don't need quotes in either JavaScript or JSON.
In a JavaScript object, it's common to put a comma
at the end of every key value pair,
even if it's the last one in the structure.
However, JSON does not support trailing commas.
So the final key value pair cannot have a comma after it
invalid JSON.
And so here I have the same number of key value pairs
and on the left in the JavaScript object,
I have a comma after each final value,
but on the right in my JSON object,
I have commas only after the first three
when they're separating two different key value pairs,
but in the final key value pair,
I cannot put a comma at the end.
Another feature that's supported in JavaScript objects
but not in JSON is comments.
You can insert a comment to explain or document your code
in JavaScript and it's ignored by parsers
when the code runs,
but JSON does not support any comments syntax,
so you can't include comments invalid JSON.
In my JavaScript object here on the left,
I can include a comment that says legal approval
and that's just before the approved true key value pair.
So in JavaScript I use those two slashes to say,
hey, this line's a comment.
But in my JSON, I cannot include that comment.
So I can only have the data that I want to include
as part of the object.
There are a few other technical details of JSON syntax
that you might need to understand in specific situations.
I think the JSON entry on Wikipedia does a great job
presenting the technical details in a readable way.
For a more technical reference, check out json.org.
This site created by Doug Crockford
who originally proposed the JSON standard.
And there are a few variations of JSON
that have slightly different rules.
For instance, JSON-C is a variant that supports comments.
If you're working with a system or team
that supports a variant, then it's fine to use that.
But keep in mind that for broad support
with other apps and systems,
the JSON standard is the most broadly supported notation.