Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we use JSON as a database?

Tags:

json

database

I'm looking for fast and efficient data storage to build my PHP based web site. I'm aware of MySql. Can I use a JSON file in my server root directory instead of a MySQL database? If yes, what is the best way to do it?

like image 421
sami Avatar asked Dec 16 '12 07:12

sami


People also ask

Is it okay to store JSON in database?

yes. that way, you get the required performance from searching the data-per-column fields, and grab the JSON blob to use in code when needed.

Should I use JSON or a database?

A relational database makes sense for fast and efficient storage and retrieval of data that has relational properties. JSON is a great data format because it is simple, lightweight and ideal for passing around raw data in a very basic format with a syntax suited to storing and exchanging text information.

When should I use JSON database?

A JSON database makes it possible to store data as JSON and provide it to applications in other forms. For example, it can operate as an in-memory key-value store for applications that just need quick and easy access. Or, indexing and querying can make JSON data appear as a table.


3 Answers

You can use any single file, including a JSON file, like this:

  • Lock it somehow (google PHP file locking, it's possibly as simple as adding a parameter to file open function or changing function name to locking version).

  • Read the data from file and parse it to internal data stucture.

  • Optionally modify the data in internal data structure.

  • If you modified the data, truncate the file to 0 length and write new data to it.

  • Unlock the file as soon as you can, other requests may be waiting...

  • You can keep using the data in internal structures to render the page, just remember it may be out-dated as soon as you release the file lock and other HTTP request can modify it.

Also, if you modify the data from user's web form, remember that it may have been modified in between. Like, load page with user details for editing, then other user deletes that user, then editer tries to save the changed details, and should probably get error instead of re-creating deleted user.

Note: This is very inefficient. If you are building a site where you expect more than say 10 simultaneous users, you have to use a more sophisticated scheme, or just use existing database... Also, you can't have too much data, because parsing JSON and generating modified JSON takes time.

As long as you have just one user at a time, it'll just get slower and slower as amount of data grows, but as user count increases, and more users means both more requests and more data, things start to get exponentially slower and you very soon hit limit where HTTP requests start to expire before file is available for handling the request...

At that point, do not try to hack it to make it faster, but instead pick some existing database framework (SQL or nosql or file-based). If you start hacking together your own, you just end up re-inventing the wheel, usually poorly :-). Well, unless it is just programming exercise, but even then it might be better to instead learn use of some existing framework.

like image 64
hyde Avatar answered Sep 17 '22 17:09

hyde


I wrote an Object Document Mapper to use with json files called JSON ODM may be a bit late, but if it is still needed it is open source under MIT Licence.

It provides a query languge, and some GeoJSON tools

like image 24
Richard Burkhardt Avatar answered Sep 20 '22 17:09

Richard Burkhardt


The new version of IBM Informix 12.10 xC2 supports now JSON.
check the link : http://pic.dhe.ibm.com/infocenter/informix/v121/topic/com.ibm.json.doc/ids_json_007.htm

The manual says it is compatible with MongoDB drivers.

About the Informix JSON compatibility

Applications that use the JSON-oriented query language, created by MongoDB, can interact with data stored in Informix® databases. The Informix database server also provides built-in JSON and BSON (binary JSON) data types.

You can use MongoDB community drivers to insert, update, and query JSON documents in Informix.

Not sure, but I believe you can use the Innovator-C edition (free for production) to test and use it with no-cost either for production enviroment.

like image 39
ceinmart Avatar answered Sep 16 '22 17:09

ceinmart