×

Python JSON

In this tutorial, we will learn about JSON in the Python programming language. We will focus on its features, Guidelines to be kept in mind while writing its syntax, the data types it supports, parsing of JSON string into a python object and python object into JSON string, etc.

JSON

It is a condensation used for JavaScript object notation. It is a typescript written in JavaScript. It is a format used to store the data. It is also used for data formatting, just like XML. It is more versatile than XML.

Python language has got an innate package known as JSON.

To be able to use JSON in python, it needs to be imported first. The following way is used to import the JSON module–

import json

Features of JSON:

  • It is easy to comprehend and write.
  • It weighs less and is based on text. It is comparatively easier than XML.
  • It is extensively used to store data and as a communication format on the web.
  • Despite being derived from JavaScript, it is Language independent. This means the code written in any other programming language can be generated and parsed in JSON data.

Syntax Guidelines of JSON:

1. Data should be written in name/value pair

For example:

{“name”: “Javatpoint”}
{“exam”: “civil service”}

2. Comma acts as a data separator.

For example:

{“name”: “Javatpoint”, exam: “civil service”}
{“name”: “laptop”, company: “apple”}

3. Curly braces act as an object holder

For example:

{“name”: “laptop”, company: “apple”}

4. Square brackets act as an array holder.

val person1 = {“name”: “Nick”, “place”: “Mussorie”, Occupation”: [“Writer”, “Singer”, “dancer”]}

Data types supported in JSON

JSON supports the following six data types.

1. String

It is a primitive data type.

The string must be written within a double quotation.

Syntax:

{“name”: “Ravi”}
{“Designation”: “IAS”}
{“cadre”: “Bihar”}

2. Number

It is also a primitive data type.

It is represented in decimal and octal. It does not support hexadecimal.

Syntax:

{“salary”: 5000000}
{“age”: 40}

3. Null

It is a primitive data type.

It is a special type of value that can be assigned to any other data type, including numbers, strings, arrays, or Boolean.

Syntax:

{
“outcome”: true,
“marks”: null,
“registration” : 28
}

4. Boolean - primitives data types

This can either be true or false.

Syntax:

{“outcome”: true}

5. Object –

It is a complex data type.

It is a set of name or value pairs written within a curly bracket.

Syntax:

{key1: value1, key2: value2….}

6. Array

It is a complex data type.

It is an ordered collection of similar data enclosed in a square bracket.

Syntax:

[value1, value2 ,…..}

Example: This figure demonstrates the document written in JSON format.

Python JSON

Parsing of JSON

Parsing of JSON means converting the JSON string into an equivalent python object and also a python object into an equivalent JSON string.

Conversion from JSON to Python

Python offers a json.loads() method to convert a JSON string into a python object.

Example: In the below code, the use of json.loads() method in parsing is being demonstrated.    python-json

Python JSON

Output:

Python JSON

Conversion from Python to JSON

Method json.dumps() is used to convert python objects into JSON strings.

It can convert objects of various data types such as a dictionary, list, tuple, string, float, int, etc.

Example: In this figure, the usage of json.dumps() method is being demonstrated.

Python JSON

Output: The following figure shows the result after the python object gets converted into a JSON string with the aid of json.dumps() method.

Python JSON

Here is an illustration to show the conversion of a python object into all legal data types.

Example: The following figure shows the data types- string, int, Boolean, and array getting converted into JSON format.

Python JSON

Output: The figure below shows an output after python objects get converted into JSON strings. But the problem here is its readability. The data being shown is not easily understandable.

Python JSON

Example: The following code is written to remove the above readability issue by using proper indentations and separators.

Python JSON

Explanation: The above code intends to provide readable data after getting converted into an equivalent JSON format.

Output: The below figure shows an output after a python object gets converted into a JSON document but in a proper and enhanced readable format.

Python JSON

Related Topics

Python List insert() method

Python List insert() method The list.insert () method in Python inserts an item at the specified position. Syntax list.insert(i, x) Parameter i: This parameter represents a number specifying the position to insert the given value. x: This parameter signifies...

1 minute read.

Import Function in Python

In Python programming language, the import keyword plays an important role in the whole code because it makes one module make available in another module. Import is used to structure...

3 minutes read.

PyPi TensorFlow

It is a free open-source library for high-performative numerical computations. The architecture of TensorFlow allows us for easy deployment and computing across a varied number of platforms and from desktops...

3 minutes read.

Run exec python from PHP

PHP (which is a recursive abbreviation for PHP Hypertext Preprocessor) is one of the most broadly involved web improvement innovations on the planet. PHP code utilized for creating sites and...

4 minutes read.

Python Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python....

3 minutes read.

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.

Python Constructor

Introduction A constructor is defined as the special kind of function or method that is used for instance variables initialization during the creation of an object of a class. The constructor's task...

5 minutes read.

Python Uses

Python has advanced in recent years to rank among the programming languages that are most often used globally. It is utilized in everything, including machine learning, software testing, and website...

4 minutes read.

Writing to a CSV file in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

6 minutes read.

Performing Transaction Using Python MySQL

Transaction A Transaction contains a set of SQL commands used to change the data in the dataset. If we say transaction, it means that the data in the database has changed....

3 minutes read.

Latest Project Ideas using Python 2024

Python is one of the well-known languages for programming in the contemporary domain of computer science. The demand to adopt Python for IT purposes is steadily increasing because of its...

7 minutes read.

How to Convert Int to String in Python?

Every value we use or store in a variable in Python will have a specific data type. It describes the value's nature; based on that, Python will automatically assign a...

4 minutes read.

Creating new Database using Python MySQL

In this article, we are going to discuss how to create a new database by connecting Python and MySQL. What is a Database? The places or memory used to secure highly and...

6 minutes read.

Python Network Programming

In network programming, python plays a very important role. Python provides full support for encoding and decoding data and network protocol in its standard library. Writing a network program in...

3 minutes read.

Yield vs Return in Python

In this article, you will learn about " Yield " and " Return " in Python. You will also understand the difference between " Yield " and " Return "...

6 minutes read.

Difference between Mutable and Immutable Objects

Difference between Mutable and Immutable Objects As we all know that Python is an object-oriented programming language. Object-oriented programming approach is based on the objects and the classes’ stores the data...

4 minutes read.

Add Element to Tuple in Python

Python: Popular high-level, all-purpose programming language Python. The new version of the Python programming language, Python 3, is used for all software applications, including web development. Python is the best programming...

4 minutes read.

Python ord() Function

Python ord() Function The ord() function in Python returns the number representing the Unicode code of a specified character. Syntax ord(c) Parameter c: This parameter represents a string or any character. Return This function returns the number...

1 minute read.

Python Matrix Operations

Python Matrix In this section of the Python tutorial, we will look at the introduction of the matrix in Python programming. We will perform many operations on the Python matrix using...

21 minutes read.

Sentence to python vector

Conversion of a Sentence to Vector in Python Before starting the tutorial, let’s just recap about the vector and the respective package that has to be imported in Python. Python Vector: Putting simply,...

3 minutes read.