RAS Playground 日本語 GitHub

RAS standard library

JSON (json)

The RAS standard library module for reading and writing JSON. An object is represented as Map<string, any> (a Scripting.Dictionary that keeps key order), an array as any[] (0-based), strings, numbers (int / double) and booleans as themselves, and null as null (Empty). It is emitted into the VBA module RAS_Json.

import * as json from "json";

export function loadSettings(p: string): string {
    const v = json.read(p);                                    // detects the encoding
    if (!json.isMap(v)) return "";
    const settings = v as Map<string, any>;                    // an object is a Map<string, any>
    const items = settings.get("items") as any[];              // an array is an any[] (0-based)
    debug.print(items.length, json.get(v, "items[0].name"), json.get(v, "owner.mail") ?? "(none)");
    settings.set("updated", now());
    json.write(p, settings);                                   // indent 2, UTF-8 without BOM
    return json.stringify(settings);                           // single-line JSON
}

Values are any, so give them a type with as Map<string, any> / as any[] before use. instanceof cannot narrow to Map, so check for an object with json.isMap(v) and for an array with isArray(v).

Reference

Function json.parse

function parse(text: string): any

Parses a JSON string into a value. Objects become Map<string, any> (keeping the key order), arrays any[] (0-based), numbers int (integers up to 9 digits) or double, strings and booleans stay as they are, and null is null. Invalid syntax raises an error with the position (line and column). To take values out, give them a type with as (v as Map<string, any>) or use json.get(v, "items[0].name").

Example: const order = json.parse(text) as Map<string, any>;, const items = order.get("items") as any[];

json.ras

Function json.stringify

function stringify(value: any, indent: int = 0): string

Converts a value to a JSON string. Map<string, any> (and Scripting.Dictionary) becomes an object, arrays (and Collection) become arrays, 2-D arrays (such as range.toArray()) become arrays of row arrays, dates become "2026-04-01" ("2026-04-01T09:30:00" when they have a time), and null and cell error values become null. With indent set to a number of spaces (2 and so on), the output is broken into indented lines (default 0: one line). Struct and class instances and arrays of 3 or more dimensions cannot be converted (an error).

Example: json.stringify(order), json.stringify(Sheet1.range("A1:C3").toArray(), 2)

json.ras

Function json.isMap

function isMap(v: any): boolean

Whether the value is a JSON object (Map<string, any> / Scripting.Dictionary). Used to check whether a value from parse is an object or an array (for arrays, isArray(v)).

Example: if (json.isMap(v)) { const m = v as Map<string, any>; … }

json.ras

Function json.get

function get(value: any, path: string): any

The value at path (null if there is none). path joins keys with ., and array elements are given as [0] or .0 ("items[0].name", "a.b.c"). For 2-D arrays (such as range.toArray()), give two indexes, row and column ("[1][2]"). Out-of-range indexes and arrays of 3 or more dimensions give null. An empty path gives value itself.

Example: json.get(order, "customer.name"), json.get(order, "items[1].price") ?? 0

json.ras

Function json.read

function read(p: string, encoding: string = "auto"): any

Reads a JSON file (giving the same value as parse). encoding is as in fs.readText (default "auto": detects BOM, UTF-8 and Shift_JIS).

Example: const settings = json.read(path.join(thisWorkbook.path, "settings.json")) as Map<string, any>;

json.ras

Function json.write

function write(p: string, value: any, indent: int = 2, encoding: string = "utf8"): void

Writes a value to a JSON file (in the form of stringify; indent defaults to 2). encoding is as in fs.writeText (default "utf8": UTF-8 without a BOM).

Example: json.write("settings.json", settings)

json.ras