简体   繁体   中英

How to return a LONGTEXT field as JSON in MySQL/MariaDB

I have a products table in which I store the product image paths as a JSON string (LONGTEXT type in MariaDB), however, when I query the database for the list of products using Node.js and send the data to the client-side, it receives the image paths as a string instead of array objects.

Question: is there a way of telling MySQL/MariaDB to return the LONGTEXT fields as JSON, or I will need to create a helper function in Node.js to do this job?

You could try using a convert() JSON

 CONVERT( your_long_text_col,  JSON)

test

select concat('"', CONVERT( '{"id": 1, "name": "Monty"}',  JSON), '"');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM