Obtaining JSON attribute values and XML tag attribute values for Oracle and MySQL databases

Obtaining JSON attribute values ​​and XML tag attribute values ​​for Oracle and MySQL databases

In daily development, we often encounter a field in the database that stores a field in clob format or large text format, which can be json or xml text. We have stored data in it, and it is always a headache when we want to get the value of one of the attributes or tags. The editor compiled some knowledge, I hope it can help everyone!

1. Obtain an attribute value in JSON format

1.1 Obtain a certain attribute value of JSON in Oracle database

Oracle database provides a function: json_value( 变量1, 变量2)
- variable 1 is stored in json format 列字段
- variable 2 is the json attribute to be obtained 层级(to be $.开头标识)
Let's test:
add a piece of JSON data in the remark field of table1

{
    
    
	"a": "aaaa",
	"b": "bbbb",
	"c": "cccc",
	"d": {
    
    
		"da": "daaaad",
		"db": "dbbbbd",
		"dc": "dccccd"
	}
}

b属性I now want to get the value of and the value in the json character dc属性, SQLI can write like this:

select
  json_value(a.remark, '$.b') as b,
  json_value(a.remark, '$.d.dc') as dc
from
  table1 a

search result:
json query result

1.2 Get the attribute value of JSON in the MySQL database

The MySQL database provides a function: json_extract(变量1,变量2)
- Variable 1 is stored in json format 列字段
- Variable 2 is the json attribute to be obtained 层级(to end with $.开头标识)
** Note: ** Let's test ( json的测试数据同上) now I want
to get the json character in b属性The value and dc属性the value SQLcan be written like this:

select
  JSON_EXTRACT(a.remark, '$.b') as b,
  JSON_EXTRACT(a.remark, '$.d.dc') as dc
from
  table1 a

search result:
Obtain attribute value results in JSON format in mysql

From the query results, we can see that the queried value is ("")wrapped in double quotes, which is obviously not what we want.
We can avoid this problem by using another function provided by MySQL:json_unquote(变量)

Transformed SQL:

select
  JSON_UNQUOTE(JSON_EXTRACT(a.remark, '$.b')) as b,
  JSON_UNQUOTE(JSON_EXTRACT(a.remark, '$.d.dc')) as dc
from
  table1 a

search result:
correct query result

2. Obtain the attribute value of a tag in XML format

2.1 Obtain the attribute value of an XML tag in the Oracle database

Oracle database provides two functions: extractvalue(变量1,变量2), : for - variable 1: to store in xml format - variable 2: to get the XML attribute (to be ) : used to identify a certain column (field) is the xml data type - variable : Indicates that the column (field) is the following test: Add a piece of XML data in the remark field of the table1 tablexmltype(变量)
extractvalue(变量1,变量2)获取XML的标签属性值
列字段
层级/开头标识
xmltype(变量)
xml的数据类型

<?xml version="1.0" encoding="UTF-8" ?>
<body>
    <a>aaaa</a>
    <b>bbbb</b>
    <c>cccc</c>
    <d>
        <da>daaaad</da>
        <db>dbbbbd</db>
        <dc>dccccd</dc>
    </d>
</body>

b属性I now want to get the value of and the value in the XML data dc属性, SQLI can write like this:

select
  extractvalue(xmltype(a.remark), '/body/b') as b,
  extractvalue(xmltype(a.remark), '/body/d/dc') as dc
from
  table1 a

search result:
xml result

2.2 Obtain the attribute value of an XML tag in the MySQL database

Oracle database provides a function: - variable 1: store in xml format - variable 2: the XML attribute to be obtained (to be ) ** Note: ** Let's test ( ) now I want to get the XML data in The value and the value can be written like this:extractvalue(变量1,变量2)
列字段
层级/开头标识
xml的测试数据同上
b属性dc属性SQL

select
  extractvalue(a.remark, '/body/b') as b,
  extractvalue(a.remark, '/body/d/dc') as dc
from
  table1 a

search result:
Xml tag attribute value in mysql

3. Summary:

Through comparison, it is not difficult to find that the methods of obtaining JSON attribute values ​​and XML tag attribute values ​​in MySQL and Oracle databases are similar.
Of course, we can also use it in combination with the actual situation. For example, if the JSON format contains the XML format, a nested method is required to obtain the XML tag attribute value in the JSON format. You can use them in combination according to your own application scenarios!

If this article is helpful to everyone, you can support and pay attention, (summary of personal experience, if there is a better way, you can comment and leave a message to communicate!)
祝大家的技术日日新,苟日新!

Guess you like

Origin blog.csdn.net/qq_42320934/article/details/129275352
Recommended