Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert nested json data in mysql table?

Tags:

json

sql

mysql

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.

like image 249
Haris Khan Avatar asked Aug 25 '17 10:08

Haris Khan


People also ask

How to use JSON_table() function in MySQL?

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?

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

How to create a JSON string by manually in MySQL?

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.

Can I use nested JSON_extract () functions in MySQL?

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.


1 Answers

JSON_APPEND serves your purpose better JSON_APPEND docs

like image 116
Carlos Gonzalez Avatar answered Oct 30 '22 06:10

Carlos Gonzalez