DEV Community

Cover image for All you need to know about JSON?
Angel Barre
Angel Barre

Posted on

All you need to know about JSON?

What is JSON?

JSON stands for JavaScript Object Notation

It is an open standard file format and data exchange format that employs text that humans can read to store and send data objects made up of attribute-value pairs and arrays (or other serializable values). It is a widely used data format for electronic data exchange, notably between servers and online applications.

JSON Types

  • Strings : "Angel Barre" "Hello World"
  • Numbers: 13 0.6 -21
  • Booleans: true, false
  • Arrays : [1,2,3] ["Hi", "Dev"]
  • Objects: {"key":"value", "name": "Angel"}

NOTE: JSON names require double quotes. JavaScript names do not.

JSON SYNTAX

  • Data is in name/value pairs
  • Commas are used to separate data
  • Curly braces hold objects
  • Square brackets hold arrays ### Example:
{
    "name":"Angel", 
    "age":20 , 
    "isDeveloper": true,
    "languages": ["HTML","CSS","JavaScript","PHP"],
    "Idk":null,
    "pets":[{"name": "Robert","type" : "Dog"},
                {"name": "Max", "type" : "Cat" }]
}

Enter fullscreen mode Exit fullscreen mode

How to access data from a JSON file or Object?

If we load this data as a variable called info, we could then access the data inside it using the same dot/bracket notation. For example:
info.name or info['name']

Output:

"Angel"
Enter fullscreen mode Exit fullscreen mode

info.pets[1].name

Output

"Max"
Enter fullscreen mode Exit fullscreen mode

info.languages[2]

Output

"JavaScript"
Enter fullscreen mode Exit fullscreen mode

Why use JSON?

The syntax of the JSON format is similar to that of the JavaScript object creation code. This makes it simple for a JavaScript code to turn JSON data into JavaScript objects.

JSON data may be used in any programming language and is easily transferable between machines because it is merely in text format.

Things to keep in mind while writing JSON

  1. JSON consists solely of a string with a predefined data format.It has no methods,only properties.
  2. JSON requires double quotes to be used around strings and property names. Single quotes are invalid.
  3. A single missed comma or colon might cause a JSON file to stop working.

Top comments (0)