> For the complete documentation index, see [llms.txt](https://structure.js.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://structure.js.org/serialization.md).

# Serialization

It's possible to obtain a serialized object of a Structure using the method `toJSON()`. This method is also compliant with the [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) specification, so you can use it to serialize your object too.

**Important:**

* Be aware that `toJSON()` will return an object, not the JSON in form of a string like `JSON.stringify()` does
* Refer to the [Date#toJSON](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON) specification to see how dates will be serialized by `JSON.stringify`
* Refer to [Dealing with nullable attributes](/schema-concept/nullable-attributes.md#nullability-and-serialization) to check how `nullables` are going to be returned on **Serialization**

```javascript
const Book = attributes({
  name: String
})(class Book { });

const User = attributes({
  name: String,
  birth: Date,
  books: {
    type: Array,
    itemType: Book
  }
})(class User { });

const user = new User({
  name: 'John Something',
  birth: new Date('10/10/1990'),
  books: [
    new Book({ name: 'The name of the wind' }),
    new Book({ name: 'Stonehenge' })
  ]
});

user.toJSON(); /* {
  name: 'John Something',
  birth: new Date('10/10/1990'),
  books: [
    { name: 'The name of the wind' },
    { name: 'Stonehenge' }
  ]
}
*/

JSON.stringify(user)); // {"name":"John Something","birth":"1990-10-10T03:00:00.000Z","books":[{"name":"The name of the wind"},{"name":"Stonehenge"}]}
```


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://structure.js.org/serialization.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
