Mark Jones
Mark Jones

Reputation: 115

Json into mysql

The aim is to save user entered JSON into a database. Now, before someone jumps at me, I know json, I know mysql and I know all the links inbetween.

The issue is: I need to safely store the ENTIRE JSON feed in a single cell in the table.

The background: this function will be a temp fix for a tool, that is needed asap but will require a lot of time. The temp fix will allow the system to go live with minimal code.

Users will create a GOOGLE maps style here ( http://gmaps-samples-v3.googlecode.com/svn/trunk/styledmaps/wizard/index.html)

and have the JSON made for them

[
  {
    "stylers": [
      { "visibility": "off" }
    ]
  },{
    "featureType": "water",
    "stylers": [
      { "visibility": "on" }
    ]
  },{
    "featureType": "transit.line",
    "elementType": "geometry.fill",
    "stylers": [
      { "visibility": "on" },
      { "hue": "#ff3300" },
      { "color": "#ff0000" },
      { "weight": 0.7 }
    ]
  },{
    "featureType": "transit.station.rail",
    "stylers": [
      { "visibility": "on" },
      { "color": "#0000ff" },
      { "weight": 4.6 }
    ]
  },{
  }
]

The site will then just call the JSON and apply it using jQuery later on. What would my ''best practice'' method be at doing this.

Upvotes: 0

Views: 149

Answers (2)

Aaron Saray
Aaron Saray

Reputation: 1177

To answer your specific question, I would agree with the poster 'tom' that you should use a TEXT column.

However, I think for ease of use, you should also use prepared statements. If you create a prepared insert statement, you can then pass in the JSON directly. This will be the best representation in your database of the exact JSON (no annoying slashes) - AND be the safest. Please don't forget to do this step - its very important!

Upvotes: 1

tom
tom

Reputation: 19163

Since MySQL doesn't have a dedicated JSON column type, I would just store the JSON in an unbounded TEXT column. Just make sure you always check for valid JSON on write.

Upvotes: 0

Related Questions