Can I use a JSON file as a database?

Asked

Viewed 4,232 times

3

I’m looking to start a project but I don’t want to create a database so I wonder if I could use a file json as a database.

  • 2

    Power can, will always depend on your design, structure, etc. Whether it is better for you and/or your application, of course. Look up "noSql", to get a sense of non-relational banks.

  • 2

    that’s basically what mongodb does, it’s not?

  • 2

    Also has the firebase Realtime database, is very simple and the documentation is simple and complete

  • 1

    The reply from @Guilhermecostamilam caters well to what you want. I just don’t understand why to do it. = ) In my view, with the database it is easier to manipulate the data.

  • 1

    It depends, if he has a good knowledge of javascript and little php, storing the data in json format and then assigning them to a variable is much simpler. What is much more complicated, is to migrate from no-sql to sql or from one database to another, in this case, from files to a database

  • 1

    @Guilhermecostamilam then, but let’s assume that you will only work in this format (json) by creating a CRUD. Wouldn’t it be easier to use a Nosql? Than opening the file, check which is the line, change, etc..

  • 1

    BS.. I am asking because I never did it. rsrs

  • 2

    Yes, it would be much simpler, for me, this is because I’ve created no-sql applications, but if you don’t even know what it is, it might not be for her. I didn’t say that json files are simpler than a no-sql database, but it’s not necessarily harder either. Like I said, it’s more of a point of view

  • basically if you create functions to facilitate queries and data insertion in a json, you created a nosql bd

Show 4 more comments

2 answers

2


Can do this with one file functions example of recording:

// Array com dados
$cliente1 = array(
    'codigo'   => '001',
    'nome'     => 'William',
    'telefone' => '012 9999-6352'
);

$cliente2 = array(
    'codigo'   => '002',
    'nome'     => 'Adriano',
    'telefone' => '012 8888-4452'
);

$cliente3 = array(
    'codigo'   => '003',
    'nome'     => 'Maria',
    'telefone' => '013 3434-4444'
);

// Atribui os 3 arrays para apenas um array
$dados = array($cliente1, $cliente2, $cliente3);

// Tranforma o array $dados em JSON
$dados_json = json_encode($dados);

// Cria o arquivo cadastro.json
// O parâmetro "a" indica que o arquivo será aberto para escrita
$fp = fopen("cadastro.json", "a");

// Escreve o conteúdo JSON no arquivo
$escreve = fwrite($fp, $dados_json);

// Fecha o arquivo
fclose($fp);

And an example of reading:

// Para o PHP 5 e superior
$handle = fopen("cadastro.json", "rb");
$contents = stream_get_contents($handle);
fclose($handle);

var_dump(json_decode($contents));

You can also assign the json of the file to a javascript variable and work with it on the front end, in which case do not use json_decode:

//...
echo "<script> let json = $contents; </script>";

But I believe you are "thinking wrong", if you want to do something simpler using a json file so you don’t have to keep changing the bank structure, business rules, etc. I suggest you just do the view part, validate it and then go to the back end. Making a system like this and then changing the database of a json file to an SQL model will give much more trbalho, it is preferable to keep the same structure (no-sql), maybe just changing from json files to some database like Mongodb or Firebase Realtime Database

  • 1

    Working json as bd directly in javascript would be a tremendous security flaw. If you are to use json as bd, you must protect it from external access and create the rules for access in the backend.

  • 1

    I believe there is no problem that, from what I understand, it is only to start the project, the final version will not be like this

0

The default configuration data of a system module I am developing is being saved in .json. So generally speaking, yes, you can use... Tip #1: If you’re using PHP, build a class to manage the CRUD of the data in the . json files used as DB. Tip #2: Create/edit a . htaccess (or create in the top directory of the .json files), to prevent the listing of directories/files this avoids the listing of the files. json used as DB. Still in . htaccess configure to prevent access to the files folder. json or direct access to your db.json file (for example), thus preventing third parties from accessing your file. json directly from the browser (for example) and only your server will have access to or your previously created PHP class. To improve the experience, do it in AJAX.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.