当前位置:网站首页>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
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
, orpath
Any one of the parameters isNULL
, Then return toNULL
Does not exist in the document
path
search_str
Not found
In the following cases, an error will be reported
json_doc
For illegalJSON
filepath
Is an illegal path expressionone_or_all
The parameter is notone
orall
Argument parsing :
json_doc
: To queryJson
file , For example, in my demand value Fieldone_or_all
: End condition of queryone
: 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_APPROVALescape_char
: Specify escape character .escape_char
Parameter must be constant when specified ( Empty or one character ), Whenescape_char
Parameter isNULL
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
representativeJSON
In the documentx
Corresponding value ,$[1].y
representativeJSON
The second data element in the documenty
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 ?
边栏推荐
- Network layer protocol hardware
- Royal Albert Hall, a popular landmark in London
- Implementation of one interview question one distributed lock every day
- Foreign SSL certificate
- MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值
- Redis implements SMS login function (II) redis implements login function
- QT 6.3.1conan software package release
- Bean creation process and lazy init delay loading mechanism
- 【Paper】2021_ Analysis of the Consensus Protocol of Heterogeneous Agents with Time-Delays
- Webots notes day 2
猜你喜欢
What is an optocoupler circuit and what should be paid attention to in actual use?
Check London attractions suitable for parents and children in winter vacation
Lambda&Stream
Wildcard SSL certificate issuing time
基于servlet+jsp+mysql实现的工资管理系统【源码+数据库】
破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)
A collection of errors encountered in machine learning with unity
A must see cruise experience in Bangkok: visit the Mekong River and enjoy the scenery on both sides of the river
PS1 Contemporary Art Center, Museum of modern art, New York
Threejs实现模拟河流,水面水流,水管水流,海面
随机推荐
Servlet lifecycle
Difference between request forwarding and redirection
【Paper】2013_ An efficient model predictive control scheme for an unmanned quadrotor helicopter
Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge
One interview question a day - the underlying implementation of synchronize and the lock upgrade process
【Paper】2020_ Research on defense and evaluation strategy of heterogeneous UAV formation_ Zuojiankai
National Museum of Singapore - give you spiritual and physical satisfaction
Spring Festival Tourism Strategy: welcome the new year in Bangkok, Thailand
PS1 Contemporary Art Center, Museum of modern art, New York
Connect to the database and run node JS running database shows that the database is missing
Stack implementation integrated Calculator - code implementation
The difference between get and post requests
File system and directory operations
The most comprehensive summary notes of redis foundation + advanced project in history
Internship: interface case implementation
Introduction to system programming
Foreign SSL certificate
Have a heart beating Valentine's day in Singapore
Lambda&Stream
SQL error caused by entity class: Oracle "ora-00904" error: possible case of invalid identifier