Hello everyone , well am using a script with javascript that gives me some values , that i decided storing in a var JSON :

{"user_IP":"192.168.*.**","page_name":"Nous suivre","path_name":"/test/suiv.html","nbre_visites":"3"}

//var s=JSON.stringify(cookieso);
//document.getElementById("affichage").innerHTML = s;

the thing , is that i wish i could store directly on my database :
P.S i m working with wampserver ,
I tried to do something with PHP but either it doesn't write anything on my database , either it's NULL .

i dont want to use a JSON file , i wanna use the data as brut at it is .
Thank's for ur help

Recommended Answers

All 4 Replies

If you're using a capable database you can create a JSON field, but as with every choice there are pros and cons.

The pros are, of course, flexibility. Providing your JSON is valid, it can be stored, no matter how complex or nested.

The cons, funnily enough, are also related to flexibility. All of the tools your database provides you with to ensure data is valid and correct are gone, many of the options that your database gives you when it comes to quick and easy querying are now more complex or slower.

My advice would be to use it sparingly. If your data is relational, and in almost all cases it will be, use a relational database. If some parts are not, then model those bits using a more-apt technology (array or JSON data types in the DB or another technology entirely). Don't think about scale, especially webscale until you need to.

You could use an object relational mapper and JSON.NET to read the JSON into a model, and then persist the data to a database table, relational style. I have never stored JSON in the actual database. The Object relational mapper could then read the object into the model, and JSON.NET could serve it up to your application as JSON. It is two way.

Woopse. Sorry I am .NET guy. Ignore the C# specific stuff, there has to be other libraries that can help you.

The beauty of JSON is that it can be either simple or complex, depending on your needs... and I think that's the point to which overwraith and pty are gearing their responses.
But looking at your JSON example, it seems like you're only using the format because you want to, not because of a specific need. If you're using PHP, why not just use json_decode? You'll need to POST the data for PHP to see it, either as a normal form post or as an Ajax, so maybe that's where your previous PHP was null?

JSON is a markup language, like XML, but vastly different in appearance. It is meant to be read into an object where you can actually use and manipulate the data. Javascript as a convenient method for turning it into a javascript object JSON.parse(jsonObj);. Bear in mind that the actual case of the command is necessary. JSON can be sent to the client, or sent to the backend. It is a serialization format. In fact it is just one representation of the data, where the backend could be relational and could represent the data equally as well with an ORM(not necesary for you). Or the backend could simply store JSON using a specialized database (mongo db). Storing the actual markup as a clob is a bit wierd, but I suppose it has probably been done before.

The object would be a direct represenation of your json with the fields as its own fields or properties, and the database table would persist these fields or properties. Backend languages require other libraries to parse json. I believe PHP uses json_decode.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.