Query JSON data stored in a MySQL Database

If you are storing JSON information in a MySQL database and need to perform a query based on this then you'll need this. Say you have a field in the database called extended and within it you store information for Company such as:

{"Company":"Sony","Country":"Japan"}

Then you will need the following to select the Company name from rows where the company name equals something.

$sql = "SELECT if(instr(extended,'Company'),substring(substring(extended,instr(extended,'Company')+10),1,instr(substring(extended,instr(extended,'Company')+10),'\",\"')-1),") FROM table WHERE if(instr(extended,'Company'),substring(substring(extended,instr(extended,'Company')+10),1,instr(substring(extended,instr(extended,'Company')+10),'\",\"')-1),")!="";

Tags: , ,

About Keiron

Web Developer based in the UK. Click here if you want to work with me