# tools/core/readers/json.reader.ts

> 38 lines of code and 7 definitions.

Tree: Coordination tree
Language: typescript
Canonical: https://banes-lab.com/anatomy/coordination#file-coordination-tools-core-readers-json-reader-ts
Source text: https://banes-lab.com/assets/sources/source.11f19438181b9430a398190980586175edbcc03a1b6db9fbf2907fe6d9af0ca0.generated.txt

## Definitions

- `parseJson` (lexical_declaration, line 1, exported)
- `tryParse` (lexical_declaration, line 10, exported)
- `narrow` (lexical_declaration, line 23, exported)
- `readJson` (lexical_declaration, line 35, exported)
- `detail` (lexical_declaration, line 5, exported)
- `value` (lexical_declaration, line 12, exported)
- `fieldOf` (lexical_declaration, line 19, exported)

## Used by

- [tools/core/inspectors/report.inspector.ts](https://banes-lab.com/source/coordination/tools/core/inspectors/report.inspector.ts.md)

## Source

```typescript
export const parseJson = function parseJson(text: string, origin: string): unknown {
    try {
        return JSON.parse(text);
    } catch (error) {
        const detail = error instanceof Error ? error.message : String(error);
        throw new Error(`${origin} is not readable JSON — ${detail}`, { cause: error });
    }
};

export const tryParse = function tryParse(text: string): { readonly value: unknown } | null {
    try {
        const value: unknown = JSON.parse(text);
        return { value };
    } catch {
        return null;
    }
};

export const fieldOf = function fieldOf(value: object, key: string): unknown {
    return Object.getOwnPropertyDescriptor(value, key)?.value;
};

export const narrow = function narrow<T>(
    value: unknown,
    guard: (candidate: unknown) => candidate is T,
    origin: string,
    shape: string,
): T {
    if (!guard(value)) {
        throw new Error(`${origin} does not satisfy ${shape}`);
    }
    return value;
};

export const readJson = function readJson<T>(
    text: string,
    guard: (candidate: unknown) => candidate is T,
    origin: string,
    shape: string,
): T {
    return narrow(parseJson(text, origin), guard, origin, shape);
};
```
