Logo

Programming-Idioms

  • Scheme
  • Js

Idiom #91 Load JSON file into object

Read from the file data.json and write its content into the object x.
Assume the JSON data is suitable for the type of x.

const fs = require('fs');
const x = JSON.parse(fs.readFileSync('./data.json'));
const x = require('./data.json');

require() caches when requiring a file for the first time and then uses that cache for future require() calls, so use fs.readFileSync() if the content of the JSON file changes during runtime.
(require '[clojure.java.io :refer [file]])
(require '[jsonista.core :refer [read-value]])
(def x (read-value (file "data.json")))

New implementation...