Difference btw JSON and BSON ?

 BSON vs JSON- What's the difference between them


BSON-

  • BSON stands for Binary JSON. It is the data storage and network transfer format used internally by MongoDB. 
  • BSON is similar to JSON (JavaScript Object Notation), but it's in a binary format, meaning it stores data in 1s and 0s (machine-readable), not plain text.
  • BSON (Binary JSON) is a binary-encoded serialization format used by MongoDB to store documents and make remote procedure calls. BSON extends the JSON model to provide additional data types, efficient encoding, and fast parsing.

Why BSON?

  • MongoDB needed a format that:
  • Could store rich data types (like Date, Binary, etc.)
  • Is fast to parse
  • Is compact in storage
  • Supports traversing large data quickly

   So, instead of using JSON (which is text-based and limited), MongoDB uses BSON under the hood.

Internal Use in MongoDB-

  • Data Storage: All collections in MongoDB are stored as BSON documents.
  • Data Transfer: BSON is also used to transfer data between the MongoDB server and clients.
  • Indexing: BSON structure allows MongoDB to efficiently index and search documents.

ROLE OF BSON in MongoDB-

  • Full Form -   Binary JSON
  • Used For  -       Document storage and transfer in MongoDB
  • Advantages -    Faster, supports more types, optimized for machine parsing
  • Limitations -    Slightly larger than JSON due to metadata overhead
Compatible with -  MongoDB drivers in many languages auto-handle BSON

For example code in BSON-





What is JSON?
  • JSON stands for JavaScript Object Notation. 
  • Easy for humans to read and write.
  • Easy for machines to parse and generate.
  • Based on key-value pairs, similar to Python dictionaries or JavaScript objects.
Role of JSON in MongoDB-
  • MongoDB is a NoSQL database that stores data in a flexible, JSON-like format called BSON (Binary JSON).
  • But when you're interacting with MongoDB:
  • You usually use JSON syntax to create, read, update, or delete data.
  • MongoDB's query language is based on JSON-style documents.

JSON in MongoDB is used for:
  • Storing documents (records)
  • Structuring data
  • Querying collections
  • Performing updates and filters .
For example- code in JSON Document


DIFFERENCE BETWEEN JSON and BSON-


Feature                      JSON (java script object notation)                          BSON (Binary JSON) 

 Format                         Text-based (UTF-8)                                           Binary (Machine readable)                            

Human readable               Yes                                                          No (not directly readable by users)

Used in mongodb            Used for input/output                 Used for internal storage and data transformation 

 Speed                           Slower to parse and process                             Faster due to binary format  

  Datatypes                    limited(string,number,boolean)               Extended(objectld,date,binaryint32,int64) 

  Used in APIs                   Frequently used in REST APIs                     Not used directly in APIs

Supports Date?                     Only as string                                                  As actual date object

Size                                  Smaller (No type metadata)                                Slightly larger


How They Work Together in MongoDB:

  • You write: Queries and documents using JSON-like syntax.
  • MongoDB converts it: Internally, MongoDB stores and transmits it as BSON.
  • You receive: Responses in JSON-like format, even though internally it’s BSON.

JSON BSON Key Differences-



Code that shows up the differences ( JSON and BSON)-





Summary:

  • JSON: Easy for humans and used for communication (e.g., API calls, writing queries).
  • BSON: Used by MongoDB to store and transmit data more efficiently and with more type support.









Name-Siddhi Bhosale 

University: Sri Balaji University, Pune

School: School of Computer Studies

Course: BCA (Bachelor of Computer Applications)

Interests: NoSQL, MongoDB, and related technologies

πŸ“Έ Instagram πŸ”— LinkedIn 🌐 Official Website

Comments

Post a Comment

Popular posts from this blog

MongoDB Master Guide

Covered Queries and Index Queries in MongoDB