I am trying to update my mysql table and insert json
data to my mysql
table's json-datatype
column using JSON_INSERT
. Here is the structure of my column.
{
"Data":
[{
"Devce": "ios",
"Status": 1
}]
}
This is the query I am using to insert
more data to this field.
UPDATE table SET `Value` = JSON_INSERT
(`Value`,'$.Data','{\"Device\":\"ios\",\"Status\":1}') WHERE Meta = 'REQUEST_APP'
This is supposed to update the field to this:
{
"Data":
[{
"Devce": "ios",
"Status": 1
},
{
"Devce": "ios",
"Status": 1
}
]
}
But instead it the result is:
0 rows affected. (Query took 0.0241 seconds.)
Any help regarding this would be appreciated.
In MySQL 8.0.4 and later, one such function— JSON_TABLE () —is supported. JSON_TABLE ( expr , path COLUMNS ( column_list) [AS] alias) Extracts data from a JSON document and returns it as a relational table having the specified columns. The complete syntax for this function is shown here: expr: This is an expression that returns JSON data.
How JSON data is stored in MySQL 8.0 JSON data in MySQL is treated as its own data type, a JSON string, and can appear in 2 main forms: Key-value object: a single record which consists of multiple named or indexed fields (or keys) paired with values Nested Array / Table: a table built with multiple key-value objects in a hierarchical format
Here we create a json string by manually. Here we use a python MySQLUtil class to operate mysql. The class is in: We can connect mysql using MySQLUtil instance first. We will save json data into test database. We can use mysql.execSql () to insert json data. Here is an example. After inserting json, we should close mysql connection.
If you attempt to use nested JSON_EXTRACT () functions, your query might implode. Luckily, MySQL 8.0 has the JSON_TABLE () function which is capable of unnesting (or flattening) nested arrays. The first thing you should note is that unlike the JSON_EXTRACT () function which is a SELECT argument, JSON_TABLE () is a FROM argument.
JSON_APPEND
serves your purpose better JSON_APPEND docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With