> ## Documentation Index
> Fetch the complete documentation index at: https://new.docs.falkordb.com/llms.txt
> Use this file to discover all available pages before exploring further.

# json.toJson

> Serializes a value (object, array, string, number, boolean, or null) to a JSON string.

## Description

Serializes a value to a JSON string. Handles various data types including objects, arrays, strings, numbers, booleans, and `null`.

## Syntax

```cypher theme={null}
flex.json.toJson(value)
```

## Parameters

| Parameter | Type | Required | Description                    |
| --------- | ---- | -------- | ------------------------------ |
| `value`   | any  | Yes      | The value to serialize to JSON |

## Returns

**Type:** string

A JSON string representation of the value. Returns `null` if serialization fails or if the input is `undefined`.

## Examples

### Example 1: Serialize a Map

```cypher theme={null}
WITH {name: 'Alice', age: 30, active: true} AS user
RETURN flex.json.toJson(user) AS json
```

**Output:**

```text theme={null}
json
---------------------------------------
'{"name":"Alice","age":30,"active":true}'
```

### Example 2: Serialize a List

```cypher theme={null}
WITH [1, 2, 3, 4, 5] AS numbers
RETURN flex.json.toJson(numbers) AS json
```

**Output:**

```text theme={null}
json
-----------
'[1,2,3,4,5]'
```

### Example 3: Preparing Data for Export

```cypher theme={null}
MATCH (p:Product)
WITH collect({id: p.id, name: p.name, price: p.price}) AS products
RETURN flex.json.toJson(products) AS jsonExport
```

### Example 4: Storing JSON in Properties

```cypher theme={null}
MATCH (u:User {id: 123})
WITH u, {lastLogin: u.lastLogin, preferences: u.preferences} AS metadata
SET u.metadataJson = flex.json.toJson(metadata)
```

## Notes

* Returns `null` if serialization fails (e.g., circular references)
* `undefined` values are normalized to `null`
* Dates are serialized in ISO format
* Functions and symbols cannot be serialized and will cause `null` return
* Useful for API responses, data export, or storing complex structures

## See Also

* [json.fromJsonMap](/udfs/flex/json/fromJsonMap) - Parse JSON string to map
* [json.fromJsonList](/udfs/flex/json/fromJsonList) - Parse JSON string to list

## Frequently Asked Questions

<AccordionGroup>
  <Accordion title="What types can flex.json.toJson serialize?">
    It handles maps, lists, strings, numbers, booleans, and `null`. It returns `null` for unsupported types like functions or circular references.
  </Accordion>

  <Accordion title="How are null and undefined values handled?">
    `null` values are serialized as JSON `null`. `undefined` values are normalized to `null` before serialization.
  </Accordion>
</AccordionGroup>
