Introduction

JSON stands for JavaScript Object Notation. It is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. JSON is often used to transmit data between a server and a web application, as an alternative to XML. In this tutorial, we will explore the basics of JSON, including its syntax, structure, and how to use it in Python. By the end of this tutorial, you will have a solid understanding of what JSON is and how to work with it in your Python projects.

Table of Contents :

  • Introduction to JSON
  • JSON Syntax
  • JSON Data Types
  • Working with JSON in Python 

Introduction to JSON:

  • JSON stands for JavaScript Object Notation.
  • It is a lightweight format used for data interchange.
  • It is easy for humans to read and write and easy for machines to parse and generate.
  • JSON is a minimal, readable format for structuring data. 
  • It is used primarily to transmit data between a server and web application, as an alternative to XML. 
  • JSON is a syntax for storing and exchanging data. 
  • JSON is text, written with JavaScript object notation. 
  • JSON is
    • lightweight 
    • language independent 
    • platform independent 
    • self-describing
    • easy to understand
    • easy to read and write 
    • supported in many programming languages 
  • The following JSON code is an example of how we can store data about a user : 

{ 
"name": "John Smith", 
"age": 25, 
"address": { 
				"street": "123 Main Street", 
				"city": "New York", 
				"state": "NY" 
		   } 
} 



JSON Syntax :

  • JSON syntax is a set of rules for creating and formatting JSON data.
  • JSON data is written as name/value pairs, just like a Python dictionary or a JavaScript object.
  • JSON data is enclosed in curly braces  {} .
  • Each name/value pair is separated by a comma, and the name is separated from the value by a colon.

JSON Data Types :

  • JSON supports the following data types :
    • string : a sequence of characters enclosed within double quotes
    • number : a positive or negative integer or decimal
    • boolean : true or false
    • array : an ordered list of values enclosed in square brackets  [ ] 
    • object: a collection of key/value pairs enclosed in curly braces  { } 
  • JSON Example : Here's an example of a JSON object

json
{
 "name": "John",
 "age": 30,
 "isStudent": true,
 "subjects": ["English", "Maths", "Science"],
 "address": {
   				"street": "123 Main St",
   				"city": "New York",
   				"state": "NY",
   				"zip": "10001"
 			}
}


Working with JSON in Python :

  • Python has a built-in module called json for working with JSON data.
  • To encode Python objects to JSON, we can use the  json.dumps()  method.
  • Code Sample : 

import json

person = {
 "name": "John",
 "age": 30,
 "isStudent": True,
 "subjects": ["English", "Maths", "Science"],
 "address": {
   "street": "123 Main St",
   "city": "New York",
   "state": "NY",
   "zip": "10001"
 }
}

json_data = json.dumps(person)


Decoding Json data in Python : 

  • To decode JSON data to Python objects, we can use use the  json.loads()  method.
  • Code Sample : 

import json

json_data = '{"name": "John", "age": 30, "isStudent": true, "subjects": ["English", "Maths", "Science"], "address": {"street": "123 Main St", "city": "New York", "state": "NY", "zip": "10001"}}'

person = json.loads(json_data)


Prev. Tutorial : Move Files and Directories

Next Tutorial : Working with json