×

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

IPython Display

A command shell, often known as IPython is a software application that makes an operating system's features accessible to users or other applications. Based on the purpose and specific functioning...

6 minutes read.

Permutations in Python

Recursion Basic idea: for numbers of length N. N-1 items are chosen at random between 0 and then generate permutations using the remaining N-1 elements in a recursive fashion. Once you've done...

4 minutes read.

How to check version of Python

How to check version of python The versions of Python come with different kinds of features and functionalities. It is not a herculean task to keep track of the updates these...

3 minutes read.

What is Python online compiler?

The compiler is a program that is used to scan an entire high-level program (source code) and it translates the scanned program into machine code. Compilers convert .py source code into...

5 minutes read.

Python Data Structures

Python Data Structures: Python is a programming language used worldwide for various fields such as building dynamic websites, artificial intelligence and many more. However, there is data that plays a...

18 minutes read.

Python Parser

Introduction : Parsing is referred to as the processing and translation of a Python program into machine language. In general, we could indeed say that the command parse is used to...

4 minutes read.

Python Escape Characters

In this tutorial, we will learn how to use the Escape Characters in Python. Escape Character: Escape Characters are used for some special meaning in our statements. It is denoted or represented...

3 minutes read.

What are the Purposes of Python?

Python is a high-level programming language that is simple to use and easy to write, and Python is a beginner-friendly programming language. A beginner often prefers to start with Python...

6 minutes read.

Assertion Errors and Attribute Errors in Python

Assertion Error in Python In Python, the assert condition is used to continue the execution if the given statement displays true. If the assert statement displays false, it raises Assertion Error...

3 minutes read.

What does base case mean in recursion

Base cases and recursive stages are used to define recursive functions. In a primary subject, we compute the outcome right away, given the function call's arguments. In a recursive step, we...

4 minutes read.

Python lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation...

5 minutes read.

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

Poolmanager in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python Compiler

What is Compiler? The compiler is mainly a program used to convert the source code into the machine or binary code. The source code is generally a computer program written using...

6 minutes read.

Python Parse Text File

We will learn different ways of read text records in Python. TL;DR The accompanying tells the best way to read all texts from the readme.txt document into a string: with open('readme.txt') as f: lines...

5 minutes read.

Important Difference between Python 2.x and Python 3.x with Example

The comparison between Python 2 and Python 3 is given in the article that follows. Python is a computer language that can perform more tasks than other languages and is...

5 minutes read.

What is Python 2

Python is a widely used high-level language. The initial work on developing python was begun in the late 1980s. In 1989, Guido Van Rossum started to work on it. Initially,...

3 minutes read.

Python Breakpoint

Introduction In Python 3.7, a brand-new created function called breakpoint() was added. Due to the close relationship between both the executable and the code of a debugging component, debugging Python programming...

4 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.

How to Open a file in python with Path

In this tutorial, we will see rather than the traditional way of opening a file by locating or navigating it from our desktops; we will learn how to open the...

2 minutes read.