JSON
What is JSON?
JSON is made of keys and values. A key is always quoted text. A value can be text, a number, true, false, null, a list, or another object. There is no comment. A trailing comma is an error in standard JSON. It looks like a JavaScript object, but it is not the same.
What is JSON used for?
JSON is for two programs sending data to each other. A browser, a server, and a phone can read the same text.
- An API answer
- The dependency list in package.json
- An app setting
- A translation file
What can you do with JSON?
With JSON you keep a person's name, age, and list in one file. A program reads it, changes it, and turns it back into text. JSON itself does not calculate and does not draw a page. The calculation is in another language.
Is JSON hard to learn?
A small file is easy. The difficulty is a quote and a comma mistake. One comma makes the whole file useless. When objects sit inside objects, the eye loses the path. Turning user text into an object without a check is unsafe.
Advantages and limits
Advantage: a person can read it, almost every language has a library, and the file stays small. Limit: there is no comment, and a date and money are not their own type. They are text or a number. For a large table, a database is the straighter path.
Basic ideas
An object is in curly brackets. A list is in square brackets. The two can sit inside each other.
- A key is always in quotes
- Text is in quotes too
- true and false are lowercase
- An empty value is null, not empty text
A short example
This text stores a name and whether it is ready. Do not put a comma after the last key.
{
"name": "Nibras",
"ready": true
}Common questions
- Are JSON and JavaScript the same? No. JSON is only data text. JavaScript is a language.
- Can you write a comment in JSON? Not in standard JSON. If you need a comment, that is already another format.
- Is JSON a database? No. It is a file or an answer text. It is not the database itself.