//PROCESS: JSON — Bundling data for transfer

<New code>

//PROCESS: var outputJSONString = JSON.stringify( exampleObj );
— converts a data structure stored in a variable into a JSON string that represents that data as Javascript code

//PROCESS: var exampleObj = JSON.parse( JSONString );
— converts the JSON string back into an actual data structure stored in a variable

JSON is for storing or moving data

Often you want to transfer or store data.

For example, you may have a scorekeeping program that needs to save a record or who scored goals throughout the game. Or you may need to transfer data about car prices from an online database to your program.

The industry standard for this is currently JSON.

JSON stands for Javascript Object Notation. Essentially JSON converts an object into a string that looks like working Javascript code.

For example, if I have the following code:

var teacherObj = {
	lastName:	"Drapak",
	firstName:	"Dave",
	school:		"Citadel High School",
	startYear:	1996,
	retireYear:	2027,
	weakness:	"Stupid jokes"
};

Then I can convert this code into a transferrable JSON string with:

var JSONString = JSON.stringify( teacherObj );

Now JSONString holds a string that contains the contents of teacherObj as working Javascript code.

This string can now be saved in a file, or sent from one computer to another.

Turning JSON back into a variable

Turning this string back is simple.

You can parse the data and assign it to a variable:

var teacherObj = JSON.parse( JSONString );

You can now use teacherObj as a normal variable.

Learn more