JSON is, purely and simply, a data-serialisation format. That is to say, it is syntax for taking a complex set of data and turning into a string. The string, in turn, can then be turned back into a set of data.
This is useful in various ways. The primary way is in data transfer. For instance, using HTTP, you can only transfer strings of text. But what if you want to transfer an array? Or a number? Or a boolean? How do you make sure that the other end of the conversation knows what kind of data you’re transferring?
The answer is to use a data-serialisation format. This formats your data in a particular standardised way, so that the other end knows how you want it to be interpreted.
There are various options for how to do this. One would be to use the query string format:
?x=foo&y=1&z=true
This has various disadvantages, not least that it is hard to read. Another option would be XML:
<data>
<x>foo</x>
<y>1</y>
<z>true</z>
</data>
This is complex and longwinded–and this is only a simple example!
JSON, however, attempts to be both easy to read, simple and powerful. It is a subset of Javascript literal notation, so some of the syntax will be familiar.
{
"x": "foo",
"y": 1,
"z": true
}
The history and rationale behind JSON is interesting and worth reading (it’s all about the Javascript eval
function), but basically all you need to know is that it is a simple, human-readable way of transferring data in string format. And it doesn’t need to be based in Javascript in any way: any language can deal in it if it wants to (and these days many do!).
2
solved Examples of using JSON? [closed]