How to Convert MySQL Table Field Type from BLOB to JSON?
Last Updated :
16 Dec, 2021
In this article, we would be learning a MySQL query to convert a field of BLOB Data Type to JSON Data Type in a table. To execute this query, we would need to alter the table and subsequently the field's definition. We would first need to use the ALTER TABLE command to start making changes to the table.
ALTER TABLE: ALTER TABLE is a command used to add, delete, or modify fields in an existing table. It is also used to add and drop various constraints on an existing table. We have two commands for alter table, MODIFY and CHANGE.
Step 1: Database Creation
Creation of a Database is done by using the CREATE DATABASE command in MySQL.
Query:
CREATE DATABASE geek;

Step 2: Change Database in use, to one we created.
To use a particular database among multiple ones or to specify the database whose data we are interested in,we use the USE command in MySQL.
Query:
USE geek;

Step 3: MySQL Table creation
We are going to create a table in MySQL where one field has BLOB values. Please note you can do this with multiple fields in a table.
Query:
CREATE TABLE table1(ID INT,
DOCS BLOB,
REVIEW VARCHAR(100));

Step 4: Showing structure of the table
We will use the DESCRIBE table command to cross-check if our table has created a field with BLOB Data Type.
Query:
DESCRIBE table1;
Output:

Step 5: Changing field Data Type from BLOB to JSON. DOCS field would be changed as it is of the BLOB Data Type.
Method 1: USING MODIFY command
To change a field definition use MODIFY along with the ALTER command.
Query:
ALTER TABLE table1
MODIFY DOCS JSON;

Method 2: USING CHANGE
The syntax of the CHANGE command is different. After the CHANGE keyword, we name the field that has to be changed, then specify the new definition of the field, which includes the name of the changed field. Here we are changing the name of DOCS to DOCS_CHANGE.
Query:
ALTER TABLE table1 CHANGE
DOCS DOCS_CHANGE JSON;

Step 6: Use the DESCRIBE command to see the changes made for each of the commands : MODIFY and CHANGE.
DESCRIBE table1;
Output: Results after using MODIFY

Output: Results after using CHANGE
Similar Reads
How to Convert From BLOB to Text in MySQL? In this article, we will see the conversion of a BLOB to TEXT in MySQL. BLOB: It stands for Binary Large Object. It is a kind of data type in MySQL that can store files or images in the database in binary format. It has four types i.e TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. All four types are simi
3 min read
How to Convert a JSON File to Microsoft Excel? JSON file is used to store human-provided text & then it will eventually convert it to a computer-readable file. The JSON file can't be readable and that's why it is often converted to an Excel file that is readable. That's why procedural steps must be followed to easily convert the JSON file to
3 min read
How to Change the Type of a Field? Data types are essential in programming and databases by defining the kind of data a variable or object can hold. In MongoDB, data types classify and organize data by enabling developers to perform appropriate operations and ensuring data is stored correctly according to its type.In this article, We
6 min read
How to Convert JSON to Excel in Python JSON (JavaScript Object Notation) is a widely used data format that is both easy to read and write for humans and simple for machines to parse and generate. However, there may be times when we need to convert this JSON data into an Excel spreadsheet for analysis or reporting. In this article, we'll
3 min read
How to Import Data From a CSV File in MySQL? Importing data from a CSV (Comma-Separated Values) file into a MySQL database is a common task for data migration and loading purposes. CSV files are widely used for storing and exchanging tabular data. However, we cannot run SQL queries on such CSV data so we must convert it to structured tables. I
10 min read
How to Convert Blob Data to JSON in JavaScript ? When dealing with Blob data in JavaScript, such as binary data or files, we may need to convert it into JSON format for doing so JavaScript provides us with various methods as listed below. Table of Content Using FileReader APIUsing TextDecoder APIUsing FileReader APIIn this approach, we first use t
2 min read
How To Convert Python Dictionary To JSON? In Python, a dictionary stores information using key-value pairs. But if we want to save this data to a file, share it with others, or send it over the internet then we need to convert it into a format that computers can easily understand. JSON (JavaScript Object Notation) is a simple format used fo
6 min read
How to Convert BLOB into VARCHAR in MySQL? In this article, we would be learning a SQL query to convert a column of BLOB Data Type to VARCHAR Data Type. To execute this query we would need to alter the table and subsequently a column's definition. We would first need to use the ALTER TABLE command to change the table. ALTER TABLE: ALTER TABL
2 min read
How to Convert JSON to Blob in JavaScript ? This article explores how to convert a JavaScript Object Notation (JSON) object into a Blob object in JavaScript. Blobs represent raw data, similar to files, and can be useful for various tasks like downloading or processing JSON data. What is JSON and Blob?JSON (JavaScript Object Notation): A light
2 min read
How to convert tabular string to JSON using Node.js ? Tabular: Information that is displayed in a table with rows and columns is known as "tabular format". A data table is an organized and practical approach to displaying a significant amount of information that contains repeated data items. The majority of office productivity software packages, includ
5 min read