JSON to MongoDB Python: Operations Simplified 101

• February 18th, 2022

JSON to MongoDB Python

Modern applications generate flexible data that is not ideal for traditional Databases to manage. Companies have now shifted towards NoSQL Database such as MongoDB because they are schema-less Database and better alternatives for Relational Databases. MongoDB offers greater flexibility to companies for storing and retrieving their business data and performing ad-hoc queries on it.

Python is a widely-used programming language that is getting huge support from Developers to build scalable, fast, and flexible applications to meet business requirements. With the help of its Machines Learning and various Data Analytics libraries, Python is preferred for analyzing the data from MongoDB Database. MongoDB supports JSON format and now applications also fetch data in JSON format. Handling data from JSON to MongoDB Python makes the job easier for Developers. 

By importing data from JSON to MongoDB Python, Developers can make use of a huge collection of libraries including every field. Also, inserting data from JSON to MongoDB Python makes the Analysis and other operations easier. In this article, you will learn about handling data from JSON to MongoDB Python, how to insert JSON to MongoDB Python. You will also read about performing operations on the data from JSON to MongoDB Python.

Table of Contents

Prerequisites

  • Python installed on your local machine.
  • MongoDB installed on your local machine.
  • A brief knowledge of Python language.

What is MongoDB?

MongoDB logo
Image Source

MongoDB is an alternative to traditional Relational Database Management Systems (RDBMS). It is an open-source NoSQL Database that allows users to store and retrieve data in a documents-oriented and optional schema. Companies can easily manipulate, access, and manage large datasets of distributed data as it uses JSON-type format documents. MongoDB is developed by MongoDB Inc. and offers companies a flexible storage space to support modern applications.

With the help of MongoDB, companies can use it for ad-hoc queries, indexing, load balancing, aggregation, server-side JavaScript execution, and other features. MongoDB can integrate with many platforms and frameworks to accelerate the development and management of applications. It comes with its 2 editions that are free and paid. The community edition is free to use for everyone, while the Enterprise edition is paid and comes with some additional security features, an in-memory storage engine, administration, and authentication features.

Key Features of MongoDB

Some of the main features of MongoDB are listed below.

  • Schema-Less: MongoDB allows users to maintain their data with flexibility and no restrictions on the schema of the Database. A dynamic schema is a popular option for modern applications.
  • Aggregation Framework: MongoDB support joins via aggregation framework instead of directly. With the help of the aggregation framework, it performs operations on the grouped data and gets a single result, like the GROUP BY clause in SQL.
  • BSON Format: MongoDB stores all the documents in the BSON format which is a Binary Encoded format of the JSON format. 
  • Active Community: MongoDB is an open-source as well paid. It comes with strong community support because it is widely used in many applications.

To learn more about MongoDB, click here.

What is Python?

Python logo
Image Source 

Python is an open-source, general-purpose programming language developed by Guido van Rossum and released in 1991. This high-level language comes with dynamic semantics and is designed with an emphasis on code readability using indentation. Python is widely used by Developers due to its clear, small, and easy-to-write code. Companies use Python for developing web applications, backend tasks, software, Machine Learning, Artificial, and managing data.

Python is garbage collected and dynamically typed language that supports multiple programming paradigms. It offers support of a huge number of libraries and modules that cover almost every aspect of developing applications to help Developers provide more flexibility. Python is widely used by big organizations such as Facebook, Netflix, Google, Microsoft, etc. making it a dependency for them and a huge community of active users.

Key Features of Python 

Some of the main features of Python are listed below.

  • Interpreted Language: Python executes line by line making it easier to debug the code. It uses Interpreter, unlike other languages such as C++, C, Java, etc that use the compiler.
  • User-Friendly: Python is a beginner-friendly language that is easy to code and read. It makes the learning curve much shorter than other programming languages.
  • Standard Libraries Support: Python comes with a wide range of libraries that cover fields such as Scripting, Web Development, Testing, Machine Learning, etc.

To learn more about Python, click here.

Simplify Data Analysis with Hevo’s No-code Data Pipeline

Hevo Data, a No-code Data Pipeline helps to load data from any data source such as MongoDB, SaaS applications, Cloud Storage, SDK,s, and Streaming Services and simplifies the ETL process. It supports 100+ data sources (including 30+ free data sources) and is a 3-step process by just selecting the data source, providing valid credentials, and choosing the destination. Hevo not only loads the data onto the desired Data Warehouse/destination but also enriches the data and transforms it into an analysis-ready form without having to write a single line of code.

Get Started with Hevo for Free

Its completely automated pipeline offers data to be delivered in real-time without any loss from source to destination. Its fault-tolerant and scalable architecture ensures that the data is handled in a secure, consistent manner with zero data loss and supports different forms of data. The solutions provided are consistent and work with different BI tools as well.

Check out why Hevo is the Best:

  1. Secure: Hevo has a fault-tolerant architecture that ensures that the data is handled in a secure, consistent manner with zero data loss.
  2. Schema Management: Hevo takes away the tedious task of schema management & automatically detects the schema of incoming data and maps it to the destination schema.
  3. Minimal Learning: Hevo, with its simple and interactive UI, is extremely simple for new customers to work on and perform operations.
  4. Hevo Is Built To Scale: As the number of sources and the volume of your data grows, Hevo scales horizontally, handling millions of records per minute with very little latency.
  5. Incremental Data Load: Hevo allows the transfer of data that has been modified in real-time. This ensures efficient utilization of bandwidth on both ends.
  6. Live Support: The Hevo team is available round the clock to extend exceptional support to its customers through chat, E-Mail, and support calls.
  7. Live Monitoring: Hevo allows you to monitor the data flow and check where your data is at a particular point in time.
Sign up here for a 14-Day Free Trial!

Inserting Data from JSON to MongoDB Python

Now that you have understood about MongoDB and Python. In this section, you will learn about inserting data from JSON to MongoDB Python. The Python language will be used to handle MongoDB and manipulate JSON data.

The PyMongo library will be used to access MongoDB Database in Python because it contains the essential drivers required to connect Python to MongoDB. The following steps for manipulating data from JSON to MongoDB Python are listed below.

Step 1: Creating a Collection Using PyMongo

  • Open up the code editor of your choice that supports Python on your local machine.
  • From the command-line tool, install the PyMongo library by typing the command given below.
python -m pip install pymongo
  • Import the PyMongo and JSON libraries using the code given below.
import pymongo
import JSON
  • Open up the terminal and start the MongoDB instance to the default host and port by the following command given below.
mongod
  • Make sure the MongoDB instance is running properly. Now let’s create a MongoClient using the pymnogo library to the running instance. The code is given below.
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
  • Getting access to the existing Database in MongoDB can be done through the following code given below.
db = myclient.database_sample
  • Here, database_sample is the name of the MongoDB Database.
  • To create a new collection the MongoDB Database for accessing data through JSON to MongoDB Python. The sample code is given below.
collection = myclient.sample_collection

Step 2: Inserting Data from JSON to MongoDB Python

  • You can insert one or many JSON documents in MongoDB using Python. 
  • For inserting one document from JSON to MongoDB Python, you can use the insert_one function. But before that let’s load the JSON data using Python.
  • For this, a file named data.json is created for inserting data from JSON to MongoDB Python using the following data given below.
[
  {
    "_id": "620eaf735a516a0f586af39c",
    "index": 0,
    "name": "Ruby Spears",
    "gender": "female",
    "company": "BIFLEX",
    "email": "rubyspears@biflex.com"
  }
]
  • The data has only one document, so we can load and insert JSON file into MongoDB Python.
  • To load the JSON file, use the following code given below.
with open('data.json') as file:
    file_data = json.load(file)
  • Then, you can insert data from JSON to MongoDB Python using the code given below.
collection.insert_one(file_data)
  • Another JSON file named data_many.json has multiple documents. The sample data is given below.
[
  {
    "_id": "620eb3c8bc6c15b66eb90e4d",
    "index": 0,
    "name": "Kelley Talley",
    "gender": "male",
    "company": "CENTREXIN",
    "email": "kelleytalley@centrexin.com"
  },
  {
    "_id": "620eb3c87ba3e59f659d79ce",
    "index": 1,
    "name": "Rachelle Marquez",
    "gender": "female",
    "company": "DYMI",
    "email": "rachellemarquez@dymi.com"
  },
  {
    "_id": "620eb3c81e750b3ce3849c31",
    "index": 2,
    "name": "Marcie Arnold",
    "gender": "female",
    "company": "YURTURE",
    "email": "marciearnold@yurture.com"
  },
  {
    "_id": "620eb3c8a40f3cf00765390e",
    "index": 3,
    "name": "Ollie Mccray",
    "gender": "female",
    "company": "AQUACINE",
    "email": "olliemccray@aquacine.com"
  }
]
  • To insert this data in JSON to MongoDB Python, you have to use the insert_many() function.
  • First, load the JSON file using the code given below.
with open('data_many.json') as file:
    file_data = json.load(file)
  • Now, you have to insert JSON to MongoDB Python to the collection using the code given below.
collection.insert_many(file_data)

Step 3: Filtering the Data 

  •  Now, the data has been inserted into the MongoDB Database. You can use the find selector to query and filter through the Database.
  • For this, the same data will be used for querying.
r = collection.find({ "gender": "female"})
list(map(print, r))
  • The above code prints the following output is given below.
[
  {
    "_id": "620eb3c87ba3e59f659d79ce",
    "index": 1,
    "name": "Rachelle Marquez",
    "gender": "female",
    "company": "DYMI",
    "email": "rachellemarquez@dymi.com"
  },
  {
    "_id": "620eb3c81e750b3ce3849c31",
    "index": 2,
    "name": "Marcie Arnold",
    "gender": "female",
    "company": "YURTURE",
    "email": "marciearnold@yurture.com"
  },
  {
    "_id": "620eb3c8a40f3cf00765390e",
    "index": 3,
    "name": "Ollie Mccray",
    "gender": "female",
    "company": "AQUACINE",
    "email": "olliemccray@aquacine.com"
  }
]

Step 4: Saving Data from MongoDB to JSON Python

  • Now, let’s save the above output in the JSON file locally. 
  • For this, you can save the above output into a dictionary and then write the data to MongoDB Python to JSON file.
  • For this, save the query output in the variable using the code given below.
dictionary = {[
  {
    "_id": "620eb3c87ba3e59f659d79ce",
    "index": 1,
    "name": "Rachelle Marquez",
    "gender": "female",
    "company": "DYMI",
    "email": "rachellemarquez@dymi.com"
  },
  {
    "_id": "620eb3c81e750b3ce3849c31",
    "index": 2,
    "name": "Marcie Arnold",
    "gender": "female",
    "company": "YURTURE",
    "email": "marciearnold@yurture.com"
  },
  {
    "_id": "620eb3c8a40f3cf00765390e",
    "index": 3,
    "name": "Ollie Mccray",
    "gender": "female",
    "company": "AQUACINE",
    "email": "olliemccray@aquacine.com"
  }
]}

with open("output.json", "w") as outfile:
    json.dump(dictionary, outfile)
  • It will save the output query from MongoDB to JSON Python.

That’s it! You have successfully understood how to handle data from MongoDB Python to JSON file for manipulating the data in JSON file and MongoDB Database.

Conclusion 

In this article, you learnt about MongoDB, Python, and handling data from JSON to MongoDB Python. Also, you read about how to insert the JSON file into Python MongoDB and how to perform different operations on the data in MongoDB Database. Importing data from JSON to MongoDB Python helps Developers Analyze the data and build scalable, flexible, and fast applications.

Visit our Website to Explore Hevo

MongoDB Database stores valuable business data that can be used to generate insights. Companies need to analyze their business data stored in multiple data sources. The data needs to be loaded to the Data Warehouse to get a holistic view of the data. Hevo Data is a No-code Data Pipeline solution that helps to transfer data from 100+ sources to desired Data Warehouse. It fully automates the process of transforming and transferring data to a destination without writing a single line of code.

Want to take Hevo for a spin? Sign Up here for a 14-day free trial and experience the feature-rich Hevo suite first hand.

Share your experience of learning about importing data from JSON to MongoDB Python in the comments section below!

No-code Data Pipeline For your Data Warehouse