Task 334: .JSON File Format

The file format specifications for the .JSON file format are defined in RFC 8259, "The JavaScript Object Notation (JSON) Data Interchange Format." It describes JSON as a lightweight, text-based, language-independent format for data interchange, supporting primitive types (strings, numbers, booleans, null) and structured types (objects, arrays). The syntax uses structural tokens like curly braces for objects, square brackets for arrays, colons for key-value separation, and commas for item separation. Whitespace is insignificant, strings are double-quoted with escape sequences, and numbers are base-10 without leading zeros or special values like Infinity. For interoperability, object keys should be unique.

Based on the specifications, the properties of the .JSON file format intrinsic to its file system (e.g., conventions for storage, identification, and handling) are:

  • File extension: .json
  • MIME type: application/json
  • Encoding: UTF-8
  • Byte order mark: None (must not be added; parsers may ignore if present)
  • Macintosh file type code: TEXT

Two direct download links for files of format .JSON:

Here is the embeddable HTML with JavaScript for a Ghost blog (or similar) that enables drag-and-drop of a .JSON file and dumps the properties to the screen upon drop. It includes basic validation that the file ends with .json and attempts to parse it (decode/read), but primarily displays the fixed list of properties. (Note: Writing back to a file isn't possible in browser JS without additional APIs like File System Access, so that's omitted here.)

JSON File Drag and Drop
Drag and drop a .JSON file here
  1. Here is a Python class that can open a .JSON file, decode (parse) it, read data from it, write it back or to a new file, and print the properties to the console.
import json
import os

class JSONHandler:
    def __init__(self, filename):
        self.filename = filename
        self.data = None

    def open_and_decode(self):
        if not self.filename.endswith('.json'):
            raise ValueError("File must have .json extension")
        with open(self.filename, 'r', encoding='utf-8') as f:
            self.data = json.load(f)

    def read(self, key=None):
        if self.data is None:
            raise ValueError("File not opened or decoded")
        if key:
            return self.data.get(key)
        return self.data

    def write(self, new_data=None, new_filename=None):
        if new_data is not None:
            self.data = new_data
        if self.data is None:
            raise ValueError("No data to write")
        filename = new_filename or self.filename
        with open(filename, 'w', encoding='utf-8') as f:
            json.dump(self.data, f, ensure_ascii=False, indent=4)

    def print_properties(self):
        properties = [
            "File extension: .json",
            "MIME type: application/json",
            "Encoding: UTF-8",
            "Byte order mark: None",
            "Macintosh file type code: TEXT"
        ]
        for prop in properties:
            print(prop)
  1. Here is a Java class that can open a .JSON file, decode (parse) it, read data from it, write it back or to a new file, and print the properties to the console. (Uses org.json library for simplicity; assume it's in classpath.)
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class JSONHandler {
    private String filename;
    private JSONObject data;

    public JSONHandler(String filename) {
        this.filename = filename;
        this.data = null;
    }

    public void openAndDecode() throws IOException {
        if (!filename.endsWith(".json")) {
            throw new IllegalArgumentException("File must have .json extension");
        }
        try (FileReader reader = new FileReader(filename)) {
            JSONTokener tokener = new JSONTokener(reader);
            this.data = new JSONObject(tokener);
        }
    }

    public Object read(String key) {
        if (data == null) {
            throw new IllegalStateException("File not opened or decoded");
        }
        if (key != null) {
            return data.opt(key);
        }
        return data;
    }

    public void write(JSONObject newData, String newFilename) throws IOException {
        if (newData != null) {
            this.data = newData;
        }
        if (this.data == null) {
            throw new IllegalStateException("No data to write");
        }
        String targetFilename = (newFilename != null) ? newFilename : this.filename;
        try (FileWriter writer = new FileWriter(targetFilename)) {
            this.data.write(writer, 4, 0);
        }
    }

    public void printProperties() {
        String[] properties = {
            "File extension: .json",
            "MIME type: application/json",
            "Encoding: UTF-8",
            "Byte order mark: None",
            "Macintosh file type code: TEXT"
        };
        for (String prop : properties) {
            System.out.println(prop);
        }
    }
}
  1. Here is a JavaScript class (for Node.js) that can open a .JSON file, decode (parse) it, read data from it, write it back or to a new file, and print the properties to the console.
const fs = require('fs');

class JSONHandler {
    constructor(filename) {
        this.filename = filename;
        this.data = null;
    }

    openAndDecode() {
        if (!this.filename.endsWith('.json')) {
            throw new Error('File must have .json extension');
        }
        const content = fs.readFileSync(this.filename, 'utf-8');
        this.data = JSON.parse(content);
    }

    read(key) {
        if (this.data === null) {
            throw new Error('File not opened or decoded');
        }
        if (key) {
            return this.data[key];
        }
        return this.data;
    }

    write(newData, newFilename) {
        if (newData !== undefined) {
            this.data = newData;
        }
        if (this.data === null) {
            throw new Error('No data to write');
        }
        const filename = newFilename || this.filename;
        fs.writeFileSync(filename, JSON.stringify(this.data, null, 4), 'utf-8');
    }

    printProperties() {
        const properties = [
            'File extension: .json',
            'MIME type: application/json',
            'Encoding: UTF-8',
            'Byte order mark: None',
            'Macintosh file type code: TEXT'
        ];
        properties.forEach(prop => console.log(prop));
    }
}
  1. Here is a C++ class (since C does not natively support classes, but C++ does; uses nlohmann/json for parsing, assume it's included) that can open a .JSON file, decode (parse) it, read data from it, write it back or to a new file, and print the properties to the console.
#include <iostream>
#include <fstream>
#include <string>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

class JSONHandler {
private:
    std::string filename;
    json data;

public:
    JSONHandler(const std::string& fn) : filename(fn) {}

    void openAndDecode() {
        if (filename.substr(filename.find_last_of(".") + 1) != "json") {
            throw std::invalid_argument("File must have .json extension");
        }
        std::ifstream file(filename);
        if (!file.is_open()) {
            throw std::runtime_error("Could not open file");
        }
        file >> data;
        file.close();
    }

    json read(const std::string& key = "") {
        if (data.is_null()) {
            throw std::runtime_error("File not opened or decoded");
        }
        if (!key.empty()) {
            return data.value(key, json(nullptr));
        }
        return data;
    }

    void write(const json& newData = json(nullptr), const std::string& newFilename = "") {
        json toWrite = (newData.is_null()) ? data : newData;
        if (toWrite.is_null()) {
            throw std::runtime_error("No data to write");
        }
        std::string targetFilename = newFilename.empty() ? filename : newFilename;
        std::ofstream file(targetFilename);
        if (!file.is_open()) {
            throw std::runtime_error("Could not open file for writing");
        }
        file << toWrite.dump(4);
        file.close();
    }

    void printProperties() {
        std::string properties[] = {
            "File extension: .json",
            "MIME type: application/json",
            "Encoding: UTF-8",
            "Byte order mark: None",
            "Macintosh file type code: TEXT"
        };
        for (const auto& prop : properties) {
            std::cout << prop << std::endl;
        }
    }
};