# JSON Scalable Database (JSDB) API

<figure><img src="/files/ALHnyoYcVtISyjOyWuYo" alt=""><figcaption></figcaption></figure>

JSON Scalable Database (JSDB) は、クエリパラメータ `key` で指定したJSONDBに対してデータの読み込み、追加、更新、削除を行うAPIです。各操作は `action` パラメータによって指定され、レスポンスはJSON形式で返されます。

以下は「JSON Scalable Database (JSDB)」の利用用途についての説明です。

***

## JSON Scalable Database (JSDB) 利用用途

JSON Scalable Database (JSDB) は、軽量でシンプルなAPIインターフェースを通じてデータの読み書きを行いたい場合に最適なツールです。以下のような用途でご利用いただけます。

1. **小規模データベースの代替**\
   小規模なデータセットをJSON形式で管理する場合、データベースサーバーやその設定を必要とせず、JSDBを用いることで、シンプルに読み書き操作を行うことが可能です。
2. **迅速なプロトタイピング**\
   アプリケーション開発の初期段階や試作において、ユーザーデータ、設定データ、ログなどの読み書き操作をJSDBで素早く実装でき、フロントエンドやバックエンドのテストを効率的に進められます。
3. **JSONデータの動的管理**\
   APIによりJSONデータを追加、更新、削除できるため、ファイルを手動で編集することなく、クエリによる自動化されたデータ操作を実現します。特定IDのデータ検索や部分的なデータ更新も可能であり、柔軟なデータ管理をサポートします。
4. **データ共有**\
   複数のアプリケーション間でJSONデータの同期が必要な場合や、クラウド環境でのデータ共有に役立ちます。JSDBを用いてJSONファイルのデータを簡単に取得し、他のシステムやサービスと連携させることが可能です。
5. **教育目的のデータストレージ**\
   学習管理システム（LMS）やその他の教育アプリケーションにおいて、学生情報、成績データ、課題データなどの情報を一時的に保存するためのストレージとして利用可能です。

このように、JSON Scalable Database (JSDB) は、データベースやバックエンドサーバーの導入が不要な軽量アプリケーションでのデータ管理や、アジャイルな開発のための仮データベースとして広く活用することができます。

APIキーを利用すると自動的にAPIキーに紐づけられたDBが生成されます。

### エンドポイント

```
https://events-front.mixeder.net/v2/jsdb
```

### パラメータ

| パラメータ    | 必須  | 説明                                                    |
| -------- | --- | ----------------------------------------------------- |
| `key`    | はい  | APIキー(JSONデータベースのIDとなります。)                            |
| `action` | いいえ | 実行する操作の種類。`read` (デフォルト)、`add`、`update`、`delete` から選択 |
| `id`     | いいえ | 更新や削除時に使用するデータID。`read` の場合に指定すると特定のデータのみ返却           |
| `data`   | いいえ | 追加や更新時に使用するJSON形式のデータ                                 |

### 操作一覧

#### 1. 読み込み (`read`)

JSONファイル全体、または特定のIDのデータを取得します。

* **リクエスト例**:

  ```plaintext
  GET https://events-front.mixeder.net/v2/jsdb?key=example&action=read
  GET https://events-front.mixeder.net/v2/jsdb?key=example&action=read&id=1
  ```
* **レスポンス例**:

  ```json
  [
    {"id": 0, "name": "Alice", "age": 25},
    {"id": 1, "name": "Bob", "age": 30}
  ]
  ```

  または、IDが指定された場合：

  ```json
  {"id": 1, "name": "Bob", "age": 30}
  ```

#### 2. 追加 (`add`)

新しいデータをJSONファイルに追加します。

* **リクエスト例**:

  ```plaintext
  GET https://events-front.mixeder.net/v2/jsdb?key=example&action=add&data={"name":"Charlie","age":28}
  ```
* **レスポンス例**:

  ```json
  {"success": "Data added successfully."}
  ```

#### 3. 更新 (`update`)

指定したIDのデータを新しいデータで更新します。

* **リクエスト例**:

  ```plaintext
  GET https://events-front.mixeder.net/v2/jsdb?key=example&action=update&id=1&data={"name":"Bob","age":32}
  ```
* **レスポンス例**:

  ```json
  {"success": "Data updated successfully."}
  ```

#### 4. 削除 (`delete`)

指定したIDのデータを削除します。

* **リクエスト例**:

  ```plaintext
  GET https://events-front.mixeder.net/v2/jsdb?key=example&action=delete&id=1
  ```
* **レスポンス例**:

  ```json
  {"success": "Data deleted successfully."}
  ```

### エラーレスポンス

* パラメータが不足している、または無効な場合は、以下のようにエラーメッセージが返されます。

```json
{"error": "Key parameter is missing."}
{"error": "Invalid URL."}
{"error": "ID or data missing for update."}
{"error": "Data not found."}
```

### サンプルコード

#### PHPコードサンプル

**1. JSONファイルから全データを取得する**

```php
<?php
$response = file_get_contents('https://events-front.mixeder.net/v2/jsdb?key=mydatabase&action=read');
$data = json_decode($response, true);
print_r($data);
```

**2. 新しいデータを追加する**

```php
$newData = json_encode(["name" => "John", "age" => 29]);
$response = file_get_contents("https://events-front.mixeder.net/v2/jsdb?key=mydatabase&action=add&data=" . urlencode($newData));
echo $response;
```

**3. 特定のデータを更新する**

```php
$updateData = json_encode(["name" => "Alice", "age" => 26]);
$response = file_get_contents("https://events-front.mixeder.net/v2/jsdb?key=mydatabase&action=update&id=0&data=" . urlencode($updateData));
echo $response;
```

**4. データを削除する**

```php
$response = file_get_contents("https://events-front.mixeder.net/v2/jsdb?key=mydatabase&action=delete&id=0");
echo $response;
```

#### 注意点

* `key`パラメータには、JSONファイル名を指定しますが、`.json`拡張子は不要です。
* `data`パラメータに渡すデータはJSON文字列にエンコードしてから、URLエンコードします。

***


---

# 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://developer.mixeder.net/mixederpublicapis/apirifarensu/json-scalable-database-jsdb-api.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.
