0

This is exactly what happened when i select data type to json

This is exactly what happened when i select  data type to json

user207421
  • 305,947
  • 44
  • 307
  • 483

2 Answers2

3

From MariaDB website:

JSON is an alias for LONGTEXT introduced for compatibility reasons with MySQL's JSON data type. MariaDB implements this as a LONGTEXT rather, as the JSON data type contradicts the SQL standard, and MariaDB's benchmarks indicate that performance is at least equivalent.

https://mariadb.com/kb/en/json-data-type/

Beamer
  • 758
  • 5
  • 11
  • This explains the JSON type from the manual, without any context, but doesn't offer any real solution to the problem. please consider editing your comment. – Nioooooo Feb 28 '23 at 14:00
  • There is no solution because there is no problem. Read the question and my answer few more times, think about it, then please confirm your understanding and consider upvoting my answer back. – Beamer Mar 01 '23 at 16:30
-3

Solution: MariaDB doesn't have JSON, and converts it automatically to LONGTEXT

https://dbfiddle.uk/?rdbms=mariadb_10.5&fiddle=e2feaf39e86e2888d441a53bcc5add2b

So, I change mariaDb to MySQL in xamp. Now i can set Json data type

how to change: https://stackoverflow.com/a/58973750/16180226

Note: Actually I am sending request using node js APi.

here is req body: (ignore field this for testing purpouse)

Here data is the req body

Query: 'INSERT INTO table SET ?', data

    {
        "name": "Test",
        "description": [
            {
                "hello": "description"
            },
            {
                "hello": "description"
            },
            {
                "hello": "description"
            }
        ],
        "monthlySell": {
            "hello": "monthlySell"
        }
    }

}

Code to Stringify if req body key has object field:

Now i dont have to worry about stringify, it will automatically stringyfiy to store Json if a filed is object

    const keys = Object.keys(data);
    console.log(keys.length)
    for (let i=0; i<keys.length; i++)
    {
        if(typeof data[keys[i]]==='object')
        {
            console.log(typeof keys[i] +data[keys[i]] )
            data[keys[i]]=JSON.stringify(data[keys[i]])
        }
    
    }