×

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 String rstrip() method

Python String rstrip() method The string.rstrip() method in Python returns a copy of the string with trailing characters removed. Syntax string.rstrip([chars]) Parameter chars:  This argument represents a string specifying the set of characters to be...

1 minute read.

How to Program in Python on Raspberry pi?

Introduction to Python A popular programming tool with simple, complete novice syntax is Python structure of paragraphs, phrases, and words. Due to its widespread use, this has a large community that...

4 minutes read.

Python sort() function

Python provides many built-in functions for solving many problems that arise in different situations in programs. One of such methods is the sort () method. In this article, the syntax...

4 minutes read.

Program of Cumulative Sum in Python

Introduction This tutorial, explains what is meant by lists, the cumulative total, several approaches to calculating the cumulative sum of digits in a list, etc. Learn the straightforward Python codes for...

5 minutes read.

How to clear screen in Python?

How to clear screen in python There are times when we execute our program and it results in an unexpected output. The obtained result can contain some kind of garbage values...

4 minutes read.

Python Boolean

In this article, you will learn the boolean variables in python, bool() function in python, and bool operators with examples, Boolean Objects in Python. There are the only two possible values...

6 minutes read.

Python Data Types

Python Data Types: The variable in Python is used to store values, and they can hold different values. Each variable in Python has its own data-type. Since Python is a...

13 minutes read.

How to Download Python

How to Download Python Python is the most renowned programming language and developers across the globe are working on projects that are made using Python and its libraries. Here we will...

4 minutes read.

Python Static Method

Introduction to methods that are majorly used in Python: Class MethodsInstance MethodsStatic Methods Before going to the in-depth concept of Static Methods, let us briefly discuss Class Methods and Instance Methods. Class Methods The...

4 minutes read.

Python variance() function

Variance The variance is the average of the square deviations from the mean. The variance will measure the spread of the dataset from its mean or median value. The greater the...

4 minutes read.

Python List remove() method

Python List remove() method The list.remove () method in Python removes the item at the specified position in the given list. Syntax list.remove(x) Parameter x: This parameter represents the element you want to remove and accepts any type...

1 minute read.

How to get current date in python?

How to get current date in python? In Python programming language, date and time are not just a data form, but it is possible to import a method called datetime to operate...

3 minutes read.

Python Indentation

Overview The spaces at the start of a code line are known as an indentation in Python programming. Indentation is only used for readability in other programming languages like C, C++,...

8 minutes read.

Python Command line Arguments

Python Command line Arguments The command-line argument is used to the change functionality of the program. It's an extra command the programmer can use while launching a program. These commands have many uses...

7 minutes read.

How to Iterate a List in Python

As we know, a List is one of the four unique data structures available in Python; in this article, we will deep dive into understanding iterating through a list and...

3 minutes read.

Python List

The list is one of the most versatile, mutable data-structures in Python. It can store heterogeneous data or different types of data. The list contains comma-separated values (item) within the square brackets. Creating...

4 minutes read.

Python Statistical Module

Python Statistical Module The Python statistical module provides various functions that we can use in our Python program, to perform mathematical statistics operations on the numerical data given to us. The...

8 minutes read.

Python try except

Before diving right into loads of syntax we need to know what does try except is used for and how it helps users in writing programs What is Python try except? Python...

5 minutes read.

Python 2.7 data structures

The rundown information type has a few additional techniques. Here is every one of the strategies for listing objects: list.append(x): Add a thing to the furthest limit of the rundown; comparable...

9 minutes read.

Cube Root in Python

In general, the cube is a three-dimensional solid figure that has 6 square faces. It is also called a geometrical shape with six equal faces, eight vertices, and twelve edges....

3 minutes read.