How to insert JSON data into MongoDB using Python
In this article, you will learn a simple process to insert JSON data into MongoDB using the Python programming language.
JSON (JavaScript Object Notation) is a lightweight, open-standard, data-interchange format. It is easy for humans to read and write. It is used primarily to transmit data between a web application and a server. Today, it is more popular than XML. A JSON object contains data in the form of a key/value pair. The keys are strings, and the values are JSON types. They are separated by a colon. Data in JSON can be easily accessed as objects.
MongoDB is a NoSQL, open source, document-oriented database management system. This is suitable for modern Internet applications. It provides dynamic queries, scalability, secondary indexes, and fast atomic updates, and we can easily use more than one database in a project. It also supports the JSON query language. Python provides the pymongo module to easily connect with MongoDB.
Suppose we have the following JSON data that contains 'student' data-
{
"Roll": 1001,
"address": {
"city": "New York",
"postal code": "10021",
"state": "NY",
"street address": "11 1nd Street"
},
"class": 10,
"firstname": "Priska",
"lastname": "Kashyap",
"section": "A"
}
Import JSON Module
To work with JSON, we first need to import the JSON library. This library contains all the necessary methods to manipulate JSON.
import JSON
The JSON library contains a load() method to read the JSON file data. Here is the code to read the above JSON file and convert it to a dict using Python.
import json
with open('student.json') as f:
data = json.load(f)
print(data)
Complete Code
Here, we have merged the above code that we explained in chunks to convert JSON query results to MongoDB using the Python programming language.
import json
import pymongo
with open('students.json') as f:
data = json.load(f)
client = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = client["school"]
mycol = mydb["student_info"]
x = mycol.insert_one(data)
for y in mycol.find():
print(y)
The above code inserts data into Mongo and returns the following.
(env) c:\python37\Scripts\projects>jsonmongo.py
{'_id': ObjectId('5e8623744a14b811ef72eb46'), 'Roll': 1001, 'address': {'city': 'New York', 'postal code': '10021', 'state': 'NY', 'street address': '11 1nd Street'}, 'class': 10, 'firstname': 'Priska', 'lastname': 'Kashyap', 'section': 'A'}
Related Articles
Convert MongoDB Document to JSON using PythonPython code to convert table to json
How to display MongoDB data in html page
MongoDB operations in Python
Write DataFrame to csv Python
Python Pandas Tutorial
Pandas DataFrame example
Python tkinter messagebox options
Python get client IP address
Python webbrowser default browser
Tkinter documentation
Python turtle graphics code
Factorial program in Python with recursion
Python code for snake game
Python JSON Tutorial - Create, Read, Parse JSON file
Python Xmltodict Example
Python dict to XML ElementTree
Find the stop words in nltk Python
Python OpenCV Image Filtering
Python Tkinter Combobox Event Binding