JSON is essentially organized text.
For instance, consider a client-server setup like web browser - server communication. The data from the server can be converted to JSON (text) and transmitted through the network as a stream of bytes to the client (web browser). Once received by the client, it is transformed into a JavaScript object for manipulation or display.
An Example in Java
Imagine we have a straightforward class in our Java-based server application:
class Person {
String name;
int age;
}
And an instance of this class:
Person person = new Person();
person.name = "John";
person.age = 32;
If we opt to use JSON for server-to-client interactions, converting our person instance to JSON would appear like this:
String json = "{\"person\": {\"name\": \"John\", \"age\": 32} }";
This string is then written to the HTTP response, where the client reads it and deserializes it into a JavaScript object. Deserialization may involve using JSON.parse method.
var responseText = "...";
var data = JSON.parse(responseText);
The resulting data would resemble this if represented directly as a JavaScript object:
data = {
person: {
name: john,
age: 32
}
};
Thus, starting with a Java object on the server side, we serialized it to a JSON string for transmission to the client, which then deserialized it into a manageable JavaScript object.