×

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

Introducing modern python computing in simple packages

Python is the language for newly evolving technologies and has become one of the most popular computer languages in the world. Python is used in everything right from a simple...

3 minutes read.

Python Linear regression

In this tutorial, we will understand the meaning, usage, and types of Linear Regression. Further, we will comprehend the terms cost function and Optimization. Linear Regression is the widely used Machine...

3 minutes read.

Convert String to Binary in Python

String to binary The strings can be defined as the array of Unicode code characters. Binary Binary is defined as the number system which consists two symbols 0 and 101. It is base-2...

2 minutes read.

Convert int to Float in Python using Pandas

Introduction We have already learnt about how to convert float variables to int. Now let us know how to convert int variables to float So let us create another Dataset on which...

2 minutes read.

Pltpcolor in Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to...

4 minutes read.

Python Project Ideas Based On Django

Introduction If you have learned Python and you are an expert in Django, then your practical skills should be excellent in this field. If you want to check your practical skills,...

9 minutes read.

Python Tuple Methods

Python Tuple Methods Python has two built-in methods that are used for tuples. The following are the two methods: Method Description count() The tuple.count() method in Python returns the number of times a...

1 minute read.

How to comment multiple lines in python?

How to comment multiple lines in python There are two qualities that come up with every good and successful programmer- i) Indenting the code ii) Using comments in the code Let us see how...

4 minutes read.

Conditional Expressions in Python

In Python conditional expression are sometimes referred to operator called as ternary operator. Not only Python supports ternary operator but many other programming languages supports it. Ternary operator are the...

2 minutes read.

Python program to find the area of a circle

Python program to find the area of a circle This article will discuss how to find the area of a circle in Python with a given radius. The area of a...

2 minutes read.

Curdir Python

Python Programming Language 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...

3 minutes read.

Composition in Python

Composition in Python Composition is one of the important concepts of Object-oriented programming (OOPs). Composition basically enables us for creating complex types objects by combining other types of objects in the...

6 minutes read.

Mrcnn Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python md5() function

Python md5() function The md5() function in PHP calculates the md5 hash of a string. Syntax md5 ( string $str [, bool $raw_output] ) Parameter str(required)- This parameter specifies the string. raw_output(optional)- This parameter specifies a hex or binary output format: TRUE - Raw 16 character binary...

1 minute read.

Python Function

Python Function A Python function is a reusable, organized block of code that is used to perform the specific task. The functions are the appropriate way to divide an extensive program into a...

7 minutes read.

Python String join() method

Python String join() method The String.join() method in Python concatenates each element of an iterable (such as list, string and tuple) to the given string and returns the concatenated string. Syntax string.join(seq) Parameter Seq: This...

1 minute read.

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

Python Euclidean Distance

Euclidean distance is the distance between two points with whatever of dimensions. We are using NumPy library to find and calculate the Euclidean distance. The NumPy library is used for...

1 minute read.

How to write a program in python?

How to write a program in python? In python, writing programs is not a big deal. What we need to work on is our logic and some basic rules of this...

4 minutes read.