PHPackages                             muhammetsafak/jsonbase - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. muhammetsafak/jsonbase

ActiveLibrary[Database &amp; ORM](/categories/database)

muhammetsafak/jsonbase
======================

It is a library that allows to use JSon files as database.

1.0(4y ago)03MITPHPPHP &gt;=7.4

Since Apr 23Pushed 4y ago1 watchersCompare

[ Source](https://github.com/muhammetsafak/JSONBase)[ Packagist](https://packagist.org/packages/muhammetsafak/jsonbase)[ Docs](https://github.com/muhammetsafak/jsonbase)[ RSS](/packages/muhammetsafak-jsonbase/feed)WikiDiscussions main Synced today

READMEChangelog (1)DependenciesVersions (2)Used By (0)

JSONBase
========

[](#jsonbase)

It is a library that allows to use JSON files as database.

***Note :** Do not use this library for big data. This library is suitable for use only for very small data.*

Requirements
------------

[](#requirements)

- PHP 7.4 or higher
- PHP JSON Extension

Installation
------------

[](#installation)

Include the JSonBase.php file and let it know in which directory to keep the JSon files.

```
composer require muhammetsafak/jsonbase

```

Usage
-----

[](#usage)

```
require_once 'src/Base.php';
```

```
$jsonbase_dir = __DIR__ . '/data/';
$jsonbase = new \JSONBase\Base($jsonbase_dir);
```

### Table Create

[](#table-create)

For JsonBase, each table is actually a `.json` file.

```
$jsonbase->create_table("table_name");
```

Definition of `create_table()` method:

```
public function create_table(string $name): bool
```

### Insert

[](#insert)

```
$data = array("col_name" => "Value", "column" => "columnsValue");
$jsonbase->insert("table_name", $data);
```

Definition of `insert()` method

```
public function insert(string $from, array $data): bool
```

#### Multi Insert

[](#multi-insert)

```
$data = array(
    array("col_name" => "Value", "column" => "columnsValue"),
    array("col_name" => "Value2", "column" => "columnsValue2")
);
$jsonbase->multiInsert("table_name", $data);
```

Definition of `multiInsert()` method

```
public function multiInsert(string $from, array $data): bool
```

This method may throw an error if $data is not a multi-array.

### Update

[](#update)

```
$data = ["column" => "newValue", "col" => "colNewValue"];
$where = ["columnName" => "colValue"];
$jsonbase->update("table_name", $where, $data);
```

Definition of `update()` method

```
public function update(string $from, array $where, array $data = []): bool
```

### Delete

[](#delete)

```
$where = ["id" => "340"];
$jsonbase->delete("table_name", $where);
```

Definition of `delete()` method

```
public function delete(string $from, array $where): bool
```

### DROP

[](#drop)

It tries to completely delete the table and its contents.

Definition of `drop()` method

```
public function drop(string $from): bool
```

**Example**

```
$jsonbase->drop("table_name");
```

### TRUNCATE

[](#truncate)

Empties all data in a table by deleting it.

```
public function truncate(string $from): bool
```

**Example**

```
$jsonbase->truncate("table_name");
```

### Rename

[](#rename)

Changes the name of a table.

```
public function rename(string $from, string $rename): bool
```

**Example**

```
$jsonbase->rename("old_table_name", "new_table_name");
```

### Copy

[](#copy)

Allows a table to be copied along with its data.

Caution: If there is a different table with the table name to be copied, it will overwrite (delete) it to avoid conflicts.

```
public function copy(string $from, string $copyName): bool
```

**Example**

```
$jsonbase->copy("table_name", "table_name_copy");
```

### Donwload

[](#donwload)

It allows you to download the JSonBase file. This method will try to initiate a download via the browser by adding a timestamp to the filename along with its contents.

```
public function download(string $from): void
```

**Example**

```
$jsonbase->download("table_name");
```

It stops the PHP script after it. It does nothing if the requested table does not exist.

### Tables

[](#tables)

Returns the loaded table names as an array.

```
public function tables(): array
```

**Example**

```
foreach($jsonbase->tables() as $table_name){
    echo $table_name;
}
```

produces a similar output:

```
users
settings
posts

```

#### Size

[](#size)

Returns the total size of the table in bytes.

```
public function size(string $from): int
```

**Example**

```
foreach($jsonbase->tables() as $table_name){
    echo $table_name . " : " . $jsonbase->size($table_name) . ' Byte';
}
```

produces a similar output:

```
users : 503 Byte
settings : 373 Byte
posts : 4392 Byte

```

#### rowSize

[](#rowsize)

Returns the total number of rows in the table.

```
public function rowSize(string $from): int
```

**Example**

```
foreach($jsonbase->tables() as $table_name){
    echo $table_name . " : " . $jsonbase->rowSize($table_name) . ' rows';
}
```

produces a similar output:

```
users : 5 rows
settings : 12 rows
posts : 8 rows

```

### QUERY BUILD

[](#query-build)

You can use the `select()`, `from()` and `where()` methods to create a query in the JSonBase library.

***`select()`*** It is used to generate a Json query. Specifies the columns in the results from the `row()` and `rows()` methods. `*` means all columns. If more than one column is desired, they must be separated by `,`.

Definition of `select()` method

```
public function select(string $select = '*'): self
```

***`from()`*** It is used to generate a Json query. Indicates on which Json the query to be executed with `get()` method will be executed.

Definition of `from()` method

```
public function from(string $from): self
```

`$from` a table name.

***`where()`*** It is used to generate a Json query. Creates a where array for the query to be executed with the `get()` method. Multiple matches can be made using this method chaining.

Definition of `where()` method

```
public function where(string $key, string $value): self
```

`$key` a column name

`$value` value in column

Multiple where can be added using the `where()` method multiple times in a row or chaining.

The query constructed with the `from()` and `where()` methods should be executed with the `get()` method.

***`get()`*** Execute the query created with the `select()`, `from()` and `where()` methods and get the results if any. You can access the results obtained with this method with the `row()`, `rows()` methods.

Definition of `get()` method

```
public function get(): self
```

#### Results

[](#results)

You can access the results obtained by executing the `get()` method with the `row()`, `rows()` methods.

***`row()`*** Returns the first result obtained as an associative array.

Definition of `row()` method

```
public function row(): array|false
```

***`rows()`*** Returns the resulting results as an associative array.

Definition of `rows()` method

```
public function rows(): array|false
```

#### Num\_Rows

[](#num_rows)

***`num_rows()`*** Returns the number of rows affected.

Definition of `num_rows()` method

```
public function num_rows(): int
```

---

Examples
========

[](#examples)

Suppose we have a `students.json` file like the one below.

```
[
    {"id":"885","name":"Muhammet","sex":"male","age":"18"},
    {"id":"956","name":"John","sex":"male","age":"18"},
    {"id":"957","name":"Jennifer","sex":"famale","age":"17"},
    {"id":"958","name":"Elizabeth","sex":"famale","age":"19"}
]
```

**Example 1 :**

```
$jsonbase->select('name')->from('students')->where('id', '885')->get();
$size = $jsonbase->num_rows();
if($size > 0){
    $row = $jsonbase->row();
    echo $row['name'];
}else{
    echo 'Result not found!';
}
```

Output:

```
Muhammet

```

**Example 2 :**

```
$jsonbase->select('id, name')->from('students')->where('sex', 'male')->get();
$size = $jsonbase->num_rows();
if($size > 0){
    foreach($jsonbase->rows() as $row){
        echo $row['id'] . " - " . $row['name'] . "\n";
    }
}else{
    echo 'Result not found!';
}
```

Output:

```
885 - Muhammet
956 - John

```

**Example 3 :**

```
$jsonbase->select('id, name')->from('students')->where('sex', 'famale')->where('age', '17')->get();
$size = $jsonbase->num_rows();
if($size > 0){
    foreach($jsonbase->rows() as $row){
        echo $row['id'] . " - " . $row['name'] . "\n";
    }
}else{
    echo 'Result not found!';
}
```

Output:

```
957 - Jennifer

```

---

Getting Help
------------

[](#getting-help)

If you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker.

Getting Involved
----------------

[](#getting-involved)

> All contributions to this project will be published under the MIT License. By submitting a pull request or filing a bug, issue, or feature request, you are agreeing to comply with this waiver of copyright interest.

There are two primary ways to help:

- Using the issue tracker, and
- Changing the code-base.

### Using the issue tracker

[](#using-the-issue-tracker)

Use the issue tracker to suggest feature requests, report bugs, and ask questions. This is also a great way to connect with the developers of the project as well as others who are interested in this solution.

Use the issue tracker to find ways to contribute. Find a bug or a feature, mention in the issue that you will take on that effort, then follow the Changing the code-base guidance below.

### Changing the code-base

[](#changing-the-code-base)

Generally speaking, you should fork this repository, make changes in your own fork, and then submit a pull request. All new code should have associated unit tests that validate implemented features and the presence or lack of defects. Additionally, the code should follow any stylistic and architectural guidelines prescribed by the project. In the absence of such guidelines, mimic the styles and patterns in the existing code-base.

Credits
-------

[](#credits)

- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) &lt;&gt;

License
-------

[](#license)

Copyright © 2022 [MIT License](./LICENSE)

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1532d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4b6b34f3ac8938d8ee52ba3bd260680855dc5715c7b2929d9380de30d15a67dd?d=identicon)[muhammetsafak](/maintainers/muhammetsafak)

---

Top Contributors

[![muhammetsafak](https://avatars.githubusercontent.com/u/104234499?v=4)](https://github.com/muhammetsafak "muhammetsafak (2 commits)")

---

Tags

phpjsondatabasemicro datajsonbase

### Embed Badge

![Health badge](/badges/muhammetsafak-jsonbase/health.svg)

```
[![Health](https://phpackages.com/badges/muhammetsafak-jsonbase/health.svg)](https://phpackages.com/packages/muhammetsafak-jsonbase)
```

###  Alternatives

[codezero/laravel-unique-translation

Check if a translated value in a JSON column is unique in the database.

1881.0M8](/packages/codezero-laravel-unique-translation)[clouddueling/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k23.2k](/packages/clouddueling-mysqldump-php)[webparking/laravel-type-safe-collection

This package provides type-safe extension of the laravel collection, forcing a single type of object.

378.2k](/packages/webparking-laravel-type-safe-collection)[modul-is/orm

Lightweight hybrid ORM/Explorer

1119.0k](/packages/modul-is-orm)[jonas-elias/hyperf-oracle

A oracle handler for hyperf/database.

102.5k](/packages/jonas-elias-hyperf-oracle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
