banner



How To Create Json File Python

The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json. To use this feature, we import the json package in Python script. The text in JSON is done through quoted string which contains the value in key-value mapping within { }. It is similar to the dictionary in Python.

Writing JSON to a file in python

Serializing JSON refers to the transformation of data into a series of bytes (hence serial) to be stored or transmitted across a network. To handle the data flow in a file, the JSON library in Python uses dump() or dumps() function to convert the Python objects into their respective JSON object, so it makes easy to write data to files. See the following table given below.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

PYTHON OBJECT JSON OBJECT
dict object
list, tuple array
str string
int, long, float numbers
True true
False false
None null

Using json.dumps()


The JSON package in python has a function called json.dumps() that helps in converting a dictionary to a JSON object.

It takes two parameters:

  1. dictionary – name of dictionary which should be converted to JSON object.
  2. indent – defines the number of units for indentation

After converting dictionary to a JSON object, simply write it to a file using the "write" function.

Example:

import json

dictionary = {

"name" : "sathiyajith" ,

"rollno" : 56 ,

"cgpa" : 8.6 ,

"phonenumber" : "9976770500"

}

json_object = json.dumps(dictionary, indent = 4 )

with open ( "sample.json" , "w" ) as outfile:

outfile.write(json_object)

Output:

python-writing-to-json

Using json.dump()

Another way of writing JSON to a file is by using json.dump() method


The JSON package has the "dump" function which directly writes the dictionary to a file in the form of JSON, without needing to convert it into an actual JSON object.

It takes 2 parameters:

  1. dictionary – name of dictionary which should be converted to JSON object.
  2. file pointer – pointer of the file opened in write or append mode.

Example:

import json

dictionary = {

"name" : "sathiyajith" ,

"rollno" : 56 ,

"cgpa" : 8.6 ,

"phonenumber" : "9976770500"

}

with open ( "sample.json" , "w" ) as outfile:

json.dump(dictionary, outfile)

Output:

writing JSON to file using python 2

Reading JSON from a file using python

Deserialization is the opposite of Serialization, i.e. conversion of JSON object into their respective Python objects. The load() method is used for it. If you have used JSON data from another program or obtained as a string format of JSON, then it can easily be deserialized with load(), which is usually used to load from string, otherwise, the root object is in list or dict.

Using json.load()

The JSON package has json.load() function that loads the json content from a json file into a dictionary.

It takes one parameter:

  • File pointer: A file pointer that points to a JSON file.

import json

with open ( 'sample.json' , 'r' ) as openfile:

json_object = json.load(openfile)

print (json_object)

print ( type (json_object))

Output:

Reading JSON from file using python


How To Create Json File Python

Source: https://www.geeksforgeeks.org/reading-and-writing-json-to-a-file-in-python/

Posted by: gordonlievaight.blogspot.com

0 Response to "How To Create Json File Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel