what data type to store a json array in mysql (5.6) database

22,779

Solution 1

It depends on the length of the JSON data your are going to store. If it's not too long you can use VARCHAR, but with this you have the limit of 64K:

Manual says: The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.

So if you expect to have huge objects, use any of the TEXT types:

TEXT: 65,535 characters - 64 KB 
MEDIUMTEXT: 16,777,215 - 16 MB 
LONGTEXT: 4,294,967,295 characters - 4 GB 

Since Mysql 5.7.8 you're able to use the native JSON data type, though.

Solution 2

You can use text datatype to store JSON data in MySQL Database.

Share:
22,779
Wenuka
Author by

Wenuka

Updated on July 22, 2022

Comments

  • Wenuka
    Wenuka almost 2 years

    What is the data type I should use to store a json encoded array in MySQL version 5.6 where json data type is not available? So far I'm thinking to store it as a TEXT or VARCHAR. Is it how we have to store it?

  • Wenuka
    Wenuka over 7 years
    For MySQL before version 5.7, right? Thanks Anyway.
  • Rick James
    Rick James over 7 years
    JSON came with 5.7.8 in Aug 2015.
  • Leandro Curioso
    Leandro Curioso over 5 years
    TINYTEXT: 255 characters - 255 B TEXT: 65,535 characters - 64 KB MEDIUMTEXT: 16,777,215 - 16 MB LONGTEXT: 4,294,967,295 characters - 4 GB
  • Seafish
    Seafish almost 5 years
    "Have to" is too strong. OP doesn't have to; they can use VARCHAR, TEXT, MEDIUMTEXT, LONGTEXT, or even the BLOB types.