Prerequisites:MongoDB and Python,Working With JSON Data in PythonMongoDB is a cross-platform document-oriented and a non relational (i.e NoSQL) database program. It is an open-source document database, that stores the data in the form of key-value pairs.JSON stands for JavaScript Object Notation. It is an open standard file format, and data interchange format with an extension ".json", that uses human-readable text to store and transmit data objects consisting of attribute-value pairs and array data types.
To import a JSON file in MongoDB we have to first load or open the JSON file after that we can easily insert that file into the database or the collection. To load a JSON file we have to first import json in our code after that we can open the JSON file. When our file gets loaded or opened we can easily insert it into the collection and operate on that file. Let's see the example for better understanding.Example :Sample JSON used:
importjsonfrompymongoimportMongoClient# Making Connectionmyclient=MongoClient("mongodb://localhost:27017/")# databasedb=myclient["GFG"]# Created or Switched to collection# names: GeeksForGeeksCollection=db["data"]# Loading or Opening the json filewithopen('data.json')asfile:file_data=json.load(file)# Inserting the loaded data in the Collection# if JSON contains data more than one entry# insert_many is used else insert_one is usedifisinstance(file_data,list):Collection.insert_many(file_data)else:Collection.insert_one(file_data)
Output:
V
Python Introduction
Input and Output in Python
Python Variables
Python Operators
Python Keywords
Python Data Types
Conditional Statements in Python
Loops in Python - For, While and Nested Loops
Python Functions
Recursion in Python
Python Lambda Functions
Python String
Python Lists
Python Tuples
Python Dictionary
Python Sets
Python Arrays
List Comprehension in Python
Python OOP Concepts
Python Exception Handling
File Handling in Python
Python Database Tutorial
Python MongoDB Tutorial
Python MySQL
Python Packages
Python Modules
Python DSA Libraries
List of Python GUI Library and Packages
NumPy Tutorial - Python Library
Pandas Tutorial
Matplotlib Tutorial
Python Seaborn Tutorial
StatsModel Library - Tutorial
Learning Model Building in Scikit-learn
TensorFlow Tutorial
PyTorch Tutorial
Flask Tutorial
Django Tutorial | Learn Django Framework
Django ORM - Inserting, Updating & Deleting Data
Templating With Jinja2 in Flask
Django Templates
Python | Build a REST API using Flask
How to Create a basic API using Django Rest Framework ?
Python Quiz
Python Coding Practice
Python Interview Questions and Answers