> For the complete documentation index, see [llms.txt](https://funkcia.lukemorales.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://funkcia.lukemorales.io/modules/json.md).

# JSON

Funkcia provides a safe `JSON` module to work with value encoding/decoding without breaking your application.

## SafeJSON

SafeJSON provides a safe wrapper around JavaScript's native `JSON` methods, returning `Result` types instead of throwing exceptions, preserving all signature overloads of the original namespace.

#### parse

Converts a JavaScript Object Notation (JSON) string into an object, wrapped in a `Result`.

{% code overflow="wrap" %}

```typescript
import { SafeJSON } from 'funkcia/json';

//       ┌─── Result<unknown, SyntaxError>
//       ▼
const goodJson = SafeJSON.parse('{ "name": "John" }');
// Output: Ok({ name: "John" })

const invalidJson = SafeJSON.parse('{ "name": John }');
// Output: Error(SyntaxError: Unexpected token 'J', "{ "name": John }" is not valid JSON)
```

{% endcode %}

#### stringify

Converts a JavaScript value to a JavaScript Object Notation (JSON) string, wrapped in a `Result`.

```typescript
import { SafeJSON } from 'funkcia/json';

// With function replacer
//      ┌─── Result<string, TypeError>
//      ▼
const result = SafeJSON.stringify(
  { name: "John", age: 30 },
  (key, value) => typeof value === 'number' ? value.toString() : value,
  2
);
// Output: Ok('{\n  "name": "John",\n  "age": "30"\n}')

// With array replacer
//      ┌─── Result<string, TypeError>
//      ▼
const result = SafeJSON.stringify(
  { name: "John", age: 30, email: "john@example.com" },
  ['name', 'age'],
  2
);
// Output: Ok('{\n  "name": "John",\n  "age": 30\n}')
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://funkcia.lukemorales.io/modules/json.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
