Add to simplicity and might even save some valuable server time. If you're  running MySQL 4.1 or later you can use the nifty function GROUP_CONCAT()  together with the normal CONCAT() function to build all your JSON straight from  your SQL query.  
    | username | email | 
  | tommy | tommy@tommysplace.com | 
  | jane | jane@examples.com | 
  | jack | jack@iford.com | 
 Our SQL table.SELECT
   CONCAT("[",
        GROUP_CONCAT(
             CONCAT("{username:'",username,"'"),
             CONCAT(",email:'",email),"'}")
        )
   ,"]")
AS json FROM users;
[
   {username:'tommy',email:'tommy@tommysplace.com'},
   {username:'jane',email:'jane@examples.com'},
   {username:'jack',email:'jack@jiford.com'}
]
 
No comments:
Post a Comment