# Schema

[Github](https://github.com/aykutkardas/Json-Function) repository for your stars.

## Usage

#### Install

```bash
$ npm install json-function
```

#### Import

```javascript
import { schema } from 'json-function';
```

#### Usage

Example Data

```javascript
const data = [
  {
    id: 0,
    user: {
      firstname: "John",
      lastname: "Doe"
    },
    title: "Book Name"
  },
  {
    id: 1,
    user: {
      firstname: "Johnny",
      lastname: "Doe"
    },
    title: "Book Name 2"
  }
];
```

Syntax

```javascript
schema(data, {
  book: {
    id: "id",
    title: "title"
  },
  firstname: "user.firstname",
  lastname: "user.lastname"
});
```

Output

```javascript
[
  {
    firstname: "John",
    lastname: "Doe",
    book: {
      id: 0,
      title: "Book Name"
    }
  },
  {
    firstname: "Johnny",
    lastname: "Doe",
    book: {
      id: 1,
      title: "Book Name 2"
    }
  }
];
```

Apart from this wonderful method, there are also some advanced uses.

Schema has a calback that hosts useful tools.

Example Data

```javascript
const data = [
  {
    id: 0,
    user: {
      firstname: "John",
      lastname: "Doe"
    },
    title: "Book Name"
  },
  {
    id: 1,
    user: {
      firstname: "Johnny",
      lastname: "Doe"
    },
    title: "Book Name 2"
  }
];
```

Syntax

**join()**

{% hint style="warning" %}
&#x20;The default separator is a space.
{% endhint %}

```javascript
schema(data, (sc) => ({
  id: 'id',
  fullName: sc.join('user.firstname', 'user.lastname')
}));
```

Output

```
[
  {
    id: 0,
    fullName: "John Doe"
  },
  {
    id: 1,
    fullName: "Johnny Doe"
  }
];
```

To use a custom separator.

```javascript
schema(data, (sc) => ({
  id: 'id',
  fullName: sc.join('user.firstname', 'user.lastname', { separator: '_' })
}));
```

Output

```javascript
[
  {
    id: 0,
    fullName: "John_Doe"
  },
  {
    id: 1,
    fullName: "Johnny_Doe"
  }
];
```

**custom()**

Example Data

```javascript
const data = [
  {
    id: 0,
    createdAt: "2019-03-07T19:22:36+00:00",
    user: {
      firstname: "Aykut",
      lastname: "Kardaş"
    }
  },
  {
    id: 1,
    createdAt: "2019-03-02T19:22:36+00:00",
    user: {
      firstname: "John",
      lastname: "Doe"
    }
  }
];
```

Usage

```javascript
schema(data, sc => ({
  id: "id",
  fullName: sc.custom(
    (firstname, lastname) => `${firstname.toUpperCase()} ${lastname.toUpperCase()}`,
    "user.firstname",
    "user.lastname"
  ),
  createdAt: sc.custom(
    createdAt => moment(createdAt).format("MM/DD/YYYY"),
    "createdAt"
  ),
  idIncrement: sc.custom(id => id + 1, "id")
}));
```

Output

```javascript
[
  {
    id: 0,
    fullName: "AYKUT KARDAŞ",
    createdAt: "03/07/2019",
    idIncrement: 1
  },
  {
    id: 1,
    fullName: "JOHN DOE",
    createdAt: "03/02/2019",
    idIncrement: 2
  }
]
```


---

# Agent Instructions: 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:

```
GET https://worn.gitbook.io/json-function/functions/schema.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
