I think you're confusing syntax and data.
Any number of technologies can have very similar syntax, yet that similar syntax may be used for entirely different purposes, and for creating vastly different data.
when we talk about JSON, we're talking about textual data with a Unicode encoding that follows a character syntax that is meant to be used as a data transferral mechanism. That JSON data can be transferred into a wide variety of different programming environments, parsed, and then converted into actual object structures that make sense for the environment.
The reason that it was named "JavaScript Object Notation" is that its notation is largely patterned after a subset of the literal syntax used in JavaScript programs to create objects and primitive values. Sadly, this naming contributes to the confusion of JavaScript developers.
So to determine if you're dealing with JSON, what's ultimately the most important thing to think about is whether what you're doing will result in the creation of Unicode data that follows the rules of JSON syntax.
Take this example:
var foo = {"bar":"baz"};
Is that JSON? Well if it runs in a JavaScript program, it will be evaluated, and foo
will hold a reference to some memory that is not Unicode text data.
Sure we could isolate the {"bar":"baz"}
part of the code, and transfer it into its own text file that is encoded as Unicode, but then we're really not dealing with the same example anymore.
So let's say we did that. We open our text editor, make sure it's set up for Unicode encoding, and then paste in that one part of the above code. So now the entirety of our text file is this:
{"bar":"baz"}
Now we can correctly say that we have JSON data. What if I added a ;
to the end?
{"bar":"baz"};
It's no longer JSON because it has been corrupted by the ;
which is not allowed. Again, we could play around with calling it JSON except for whatever isn't valid, but really it either is or isn't valid in its entirety.
So back to a JavaScript example. Does it every make sense to refer to JSON within the syntax of a JavaScript program? Well take our original example. If we could use some JavaScript syntax to create Unicode data and make it comply with JSON syntax, then yes, we could correctly speak of having JSON in our program.
So does JavaScript let us create Unicode data? Yes, all strings in JavaScript are UTF-16 encoded. Therefore all we need to do is create a string.
var foo = '{"bar":"baz"}';
Now we wouldn't call that entire line JSON, but we could correctly say that the foo
variable refers to memory which does hold JSON data.
We could then transfer that data to a server written in an entirely different programming language, and as long as it has a JSON parser, it could parse it, and convert it to whatever object type makes sense to that server.