> For the complete documentation index, see [llms.txt](https://bxorm.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bxorm.ortusbooks.com/bxorm-2.x/reference/built-in-functions/entitytostruct.md).

# EntityToStruct

Turn an entity, or an array of entities, into structs

Turn an entity, or an array of entities, into a struct (or an array of structs), for JSON APIs and views.

By default the struct holds the id and the plain properties. Options:

* `includes`: properties, getters (without `get`) or dotted association paths to add, as a list or array: `"id,name,role.name,orders"`. An association with no path below it uses the associated entity's own defaults; with paths below it, only those fields. `"lastLoginTime:lastLogin"` renames a key. `"*"` is every plain property.
* `excludes`: names or dotted paths to leave out (`"passwordHash,orders.total"`).
* `mappers`: a struct of key to `( value, memento ) => newValue`. Mappers run last, on the keys the struct holds. An include that is neither a property nor a getter is a computed key built by its mapper.
* `defaults`: a struct of key to the value to use when the value is null. Otherwise a null becomes an empty string, and a null collection an empty array.
* `ignoreDefaults`: ignore the entity's `this.memento` default includes and excludes.
* `profile`: use a `this.memento.profiles` entry, down the whole graph.

Entities may declare mementifier's `this.memento = { defaultIncludes, defaultExcludes, neverInclude, defaults, mappers, profiles }`; the options add to it. Dates are ISO 8601 strings. An entity that already appears higher up the same branch is written as its id, so cycles end.

Unlike mementifier, an include that is neither a property, a getter nor a mapper key raises an `orm.property.unknown` error instead of being skipped. A value that is not an entity raises `orm.argument`.

See [Entities as Structs](/bxorm-2.x/working-with-entities/structs.md) for the full rules.

## Method Signature

```
EntityToStruct(entity=[Any], options=[Struct])
```

### Arguments

| Argument  | Type     | Required | Description                                                                 | Default |
| --------- | -------- | -------- | --------------------------------------------------------------------------- | ------- |
| `entity`  | `Any`    | `true`   | An entity, or an array of entities.                                         |         |
| `options` | `Struct` | `false`  | `includes`, `excludes`, `mappers`, `defaults`, `ignoreDefaults`, `profile`. |         |

Returns a struct, or an array of structs for an array of entities.

## Examples

The id and plain properties (or the entity's `this.memento` defaults):

```java
entityToStruct( entityLoadByPK( "Manufacturer", 1 ) );
// { id : 1, name : "Ford Motor Company", address : "202 Ford Way, Dearborn MI" }
```

Add associations and leave fields out:

```java
entityToStruct( user, { includes : "role.name,orders", excludes : "passwordHash,orders.notes" } );
// { id : 42, firstName : "Ann", ..., role : { name : "admin" }, orders : [ { id : 7, total : 99.5, ... } ] }
```

Rename a key, compute a key, and change a value:

```java
entityToStruct( user, {
    includes : "lastLoginTime:lastLogin,initials",
    mappers  : {
        initials : ( value, memento ) => left( memento.firstName, 1 ) & left( memento.lastName, 1 ),
        email    : ( value ) => lcase( value )
    }
} );
```

An array of entities, with a profile:

```java
entityToStruct( entityLoad( "User", { active : true } ), { profile : "export" } );
```

To read structs without loading the entities at all, use [EntityLoadAsStruct](/bxorm-2.x/reference/built-in-functions/entityloadasstruct.md).

## Related

* [EntityCriteria](/bxorm-2.x/reference/built-in-functions/entitycriteria.md)
* [EntityDelete](/bxorm-2.x/reference/built-in-functions/entitydelete.md)
* [EntityEvict](/bxorm-2.x/reference/built-in-functions/entityevict.md)
* [EntityGetDatasource](/bxorm-2.x/reference/built-in-functions/entitygetdatasource.md)
* [EntityGetDirtyProperties](/bxorm-2.x/reference/built-in-functions/entitygetdirtyproperties.md)
* [EntityGetId](/bxorm-2.x/reference/built-in-functions/entitygetid.md)
* [EntityGetMetadata](/bxorm-2.x/reference/built-in-functions/entitygetmetadata.md)
* [EntityGetName](/bxorm-2.x/reference/built-in-functions/entitygetname.md)
* [EntityGetReference](/bxorm-2.x/reference/built-in-functions/entitygetreference.md)
* [EntityIsAttached](/bxorm-2.x/reference/built-in-functions/entityisattached.md)
* [EntityIsDirty](/bxorm-2.x/reference/built-in-functions/entityisdirty.md)
* [EntityLoad](/bxorm-2.x/reference/built-in-functions/entityload.md)
* [EntityLoadAsStruct](/bxorm-2.x/reference/built-in-functions/entityloadasstruct.md)
* [EntityLoadByExample](/bxorm-2.x/reference/built-in-functions/entityloadbyexample.md)
* [EntityLoadByPK](/bxorm-2.x/reference/built-in-functions/entityloadbypk.md)
* [EntityLoadByPKOrFail](/bxorm-2.x/reference/built-in-functions/entityloadbypkorfail.md)
* [EntityLoadOrFail](/bxorm-2.x/reference/built-in-functions/entityloadorfail.md)
* [EntityLoadOrNew](/bxorm-2.x/reference/built-in-functions/entityloadornew.md)
* [EntityLoadOrSave](/bxorm-2.x/reference/built-in-functions/entityloadorsave.md)
* [EntityLoadReadOnly](/bxorm-2.x/reference/built-in-functions/entityloadreadonly.md)
* [EntityLock](/bxorm-2.x/reference/built-in-functions/entitylock.md)
* [EntityMerge](/bxorm-2.x/reference/built-in-functions/entitymerge.md)
* [EntityNameArray](/bxorm-2.x/reference/built-in-functions/entitynamearray.md)
* [EntityNameList](/bxorm-2.x/reference/built-in-functions/entitynamelist.md)
* [EntityNew](/bxorm-2.x/reference/built-in-functions/entitynew.md)
* [EntityReload](/bxorm-2.x/reference/built-in-functions/entityreload.md)
* [EntitySave](/bxorm-2.x/reference/built-in-functions/entitysave.md)
* [EntityToQuery](/bxorm-2.x/reference/built-in-functions/entitytoquery.md)
* [ORMClearSession](/bxorm-2.x/reference/built-in-functions/ormclearsession.md)
* [ORMCloseAllSessions](/bxorm-2.x/reference/built-in-functions/ormcloseallsessions.md)
* [ORMCloseSession](/bxorm-2.x/reference/built-in-functions/ormclosesession.md)
* [ORMDiagnostics](/bxorm-2.x/reference/built-in-functions/ormdiagnostics.md)
* [ORMEvictCollection](/bxorm-2.x/reference/built-in-functions/ormevictcollection.md)
* [ORMEvictEntity](/bxorm-2.x/reference/built-in-functions/ormevictentity.md)
* [ORMEvictQueries](/bxorm-2.x/reference/built-in-functions/ormevictqueries.md)
* [ORMExecuteQuery](/bxorm-2.x/reference/built-in-functions/ormexecutequery.md)
* [ORMFlush](/bxorm-2.x/reference/built-in-functions/ormflush.md)
* [ORMFlushAll](/bxorm-2.x/reference/built-in-functions/ormflushall.md)
* [ORMGetHibernateVersion](/bxorm-2.x/reference/built-in-functions/ormgethibernateversion.md)
* [ORMGetSQLFunctions](/bxorm-2.x/reference/built-in-functions/ormgetsqlfunctions.md)
* [ORMGetSession](/bxorm-2.x/reference/built-in-functions/ormgetsession.md)
* [ORMGetSessionFactory](/bxorm-2.x/reference/built-in-functions/ormgetsessionfactory.md)
* [ORMGetSessionStatistics](/bxorm-2.x/reference/built-in-functions/ormgetsessionstatistics.md)
* [ORMIsSessionDirty](/bxorm-2.x/reference/built-in-functions/ormissessiondirty.md)
* [ORMReadOnly](/bxorm-2.x/reference/built-in-functions/ormreadonly.md)
* [ORMReload](/bxorm-2.x/reference/built-in-functions/ormreload.md)


---

# 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://bxorm.ortusbooks.com/bxorm-2.x/reference/built-in-functions/entitytostruct.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.
