App Icon

JSON/BSON Format

Back to Home

Overview

JSON (JavaScript Object Notation) is a lightweight data interchange format that's easy for humans to read and write and easy for machines to parse and generate. BSON (Binary JSON) is a binary-encoded serialization of JSON-like documents that MongoDB uses for data storage and network transfer.

Technical Details

JSON Structure

  • Key-value pairs
  • Arrays and objects
  • Strings, numbers, booleans
  • Null values

BSON Features

  • Binary format
  • Type information
  • Efficient encoding
  • MongoDB native

Common Uses

  • API responses
  • Configuration files
  • Data storage
  • Web applications

Examples

JSON Example

{
    "name": "John Doe",
    "age": 30,
    "isActive": true,
    "address": {
        "street": "123 Main St",
        "city": "Anytown"
    },
    "tags": ["developer", "designer"]
}

BSON Example

Binary representation of the above JSON

Implementation

JavaScript Example

// JSON parsing and stringifying
const jsonString = '{"name":"John","age":30}';
const obj = JSON.parse(jsonString);
console.log(obj.name); // "John"

const newJson = JSON.stringify(obj);
console.log(newJson); // '{"name":"John","age":30}'

// BSON using bson package
const BSON = require('bson');
const bson = new BSON();

const doc = { name: "John", age: 30 };
const bsonBuffer = bson.serialize(doc);
const parsedDoc = bson.deserialize(bsonBuffer);
console.log(parsedDoc.name); // "John"

References