当前位置:网站首页>MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field

MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field

2022-06-30 04:45:00 Seven's code Lab

Catalog

JSON_SEARCH function

Implementation steps

1. First step : lookup WORKFLOW_APPROVAL The index number in the array

2. The second step : Replace double quotes

3. The third step : Replace with router attribute

4. Step four : Use JSON_REPLACE Conversion of function to UPDATE Statement to update


Recently, I encountered a problem in my project , stay mysql There is a... In the database table json Format string type field , There is a collection of objects , I want to put id=6 In the record of value The object attribute of the object array in the value name The value of is WORKFLOW_APPROVAL Of router Property values are set by A Change to B.

It sounds a little convoluted , Is to update a string json A property value of an object element in the format string .

JSON_SEARCH function

The solution to this problem depends on JSON_SEARCH function . Let's take a look at the usage of this function .

JSON_SEARCH(json_doc,one_or_all,search_str [,escape_char [,path] ...])

adopt JSON_SEARCH function , Returns the matching key Corresponding JSON An array of paths , If it does not exist , Then return to NULL

Use the following conditions to return NULL

  • if  json_doc ,search_str, or path Any one of the parameters is NULL, Then return to NULL

  • Does not exist in the document path

  • search_str Not found

In the following cases, an error will be reported

  • json_doc For illegal JSON file

  • path Is an illegal path expression

  • one_or_all  The parameter is not  one  or  all

Argument parsing :

  • json_doc: To query Json  file , For example, in my demand value Field

  • one_or_all: End condition of query

    • one: The search terminates after the first occurrence , And returns a path string . If not defined, which match should be considered first .

    • all: Search returns all matching path strings , Therefore, duplicate paths are not included . If there are multiple strings , They are automatically wrapped as an array . The order of array elements is uncertain .

  • search_str: Parameter value to search , For example, in my demand WORKFLOW_APPROVAL
    • escape_char: Specify escape character .escape_char  Parameter must be constant when specified ( Empty or one character ), When escape_char Parameter is NULL Or if it doesn't exist , System default use  \  As escape character .
    • path The specified scope is always the document to search for or operate on , With leading $ Character representation . The path branch consists of the period character  . Separate . The cells in the array are represented by [N] Express , The format is the same as that of normal access to array elements , for example  $.x  representative JSON In the document x Corresponding value ,$[1].y representative JSON The second data element in the document y The corresponding value .

  For specific usage, please refer to Examples of official documents

notes : stay search_str in , wildcard % and _ Can be like in LIKE It works the same way on the Internet , among % Used to match multiple characters ( Include 0), _ Only one character can be matched .

Implementation steps

1. First step : lookup WORKFLOW_APPROVAL The index number in the array

First of all, find json Array name yes WORKFLOW_APPROVAL The index number of .

select JSON_SEARCH(
               value,
               'one',
               'WORKFLOW_APPROVAL',
               null,
               '$**.name')
from hx_maintain_param
where id = 6

2. The second step : Replace double quotes

As a result of the query path The expression is quoted , If the quoted expression is directly executed, an error will be reported , Therefore use REPLACE Function remove the double quotation marks .

select REPLACE(
               JSON_SEARCH(
                       value,
                       'one',
                       'WORKFLOW_APPROVAL',
                       null,
                       '$**.name'),
                '"',
                ''
           )
from hx_maintain_param
where id = 6

3. The third step : Replace with router attribute

If you will $[35].name  This expression goes to JSON_REPLACE Function , It means that name The value of the property , But I want to replace yes router The value of the property , Therefore, you also need to use REPLACE Function name Replace with router.

select
       REPLACE(
               REPLACE(
                       JSON_SEARCH(
                               value,
                               'one',
                               'WORKFLOW_APPROVAL',
                               null,
                               '$**.name'),
                       '"',
                       ''
                   ),
                'name',
                'router'
           )
from hx_maintain_param
where id = 6

4. Step four : Use JSON_REPLACE Conversion of function to UPDATE Statement to update

The final statement :

UPDATE hx_maintain_param
SET value =
        JSON_REPLACE(
                value,
                REPLACE(
                        REPLACE(
                                JSON_SEARCH(
                                        value,
                                        'one',
                                        'WORKFLOW_APPROVAL_PASS',
                                        null,
                                        '$**.name'),
                                '"',
                                ''
                            ),
                        'name',
                        'router'
                    ),
                '/manageAudit/manageAuditMyList/detail?auditId=:auditId&title=:title&state=:state&auditState=:auditState&category=:category&secondCategory=:secondCategory'
            )
where id = 6;

I am a Seven, A tireless program , I hope this article can be beneficial to you

Figure it out SRE Work content of

Why is it that monk Sha is most likely to be dismissed in the journey to the west project team ?

To use message queuing , You should know the following ...

GC How to judge whether an object is garbage ? Deep analysis of the principle of three color marking algorithm

What determines the success of a project ?

原网站

版权声明
本文为[Seven's code Lab]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/181/202206300439318773.html