Is it correct to use JSON to store data that does not need to be in a database?

Asked

Viewed 805 times

1

Ex: In the company where I work they have put in my code a large amount of data that does not need to be in the database, but is not text or actual content. They are a lot of validations and depending on the clause it returns a certain type of content, the problem is that everything is in the same file, which greatly hinders the reading of code, usually in such cases I like to leave the data in a JSON file and only take the necessary one leaving the source code more lean.

Is this practice correct? What would be better?

  • 1

    If you can give an example it would be better. But having static data in a JSON is good practice yes and being in memory (in a variable) is quick to query.

  • @mikaellemos033 Just this? It has something to do with JSON?

  • Recently they added a chunk of code like this in my file: if( $x = 'x') { $x = 'test'; $y = 'yteste'; }elseif($x = 'w'){ $x = 'teste2'; $y = 'zteste'; } .... What I usually do is take this play dice on a JSON, and then I just take the dice I make a filter.

  • @bigown has to do with JSON, because my question is whether this practice is correct, and whether later this could lead to some problem.

  • Now you’ve put in a bigger chunk of code but I still can’t see the difficulty in doing this with JSON. If it wasn’t JSON it would probably be something else that is being manipulated there. I don’t know if it would make much difference. But it could be. It’s missing context. Somehow I gave a generic answer. Better if you have something more specific.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

Show 1 more comment

2 answers

2

It is hard to say without seeing the concrete case, abstractly there is nothing wrong, even today all modern application configuration files use this format. Used to be the famous format ini, or XML, or proprietary formats.

This may not be your case but there are already many people exchanging data in JSON format.

I would need to find out why it is hard to read and if there is anything that can be done to change it. But I doubt the problem is JSON.

Particularly I prefer more structured ways to store certain information when possible, so I prefer to leave in memory in a array or other appropriate data structure or even in tables in the database.

Even if this is in the code and it’s already in memory, I would throw the data into a data structure and avoid getting access to string of JSON all the time.

0

Power can, I believe there are no right or wrong practices, it depends a lot on what you need.

But the question is whether you will need to consult this all the time from the file. Reading the file all the time can cause unnecessary slowness.

What is interesting is to do as Mr Sérgio mentioned in his comment. Import this data into memory (leaving it in the web session or static in the application).

So the reading won’t be slow, especially if you need it all the time.

Browser other questions tagged

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