简体   繁体   English

删除JSON键中的前导和尾随空格

[英]Remove leading and trailing whitespaces in JSON keys

I am trying to get a JSON value from my MariaDB server using JSON_EXTRACT. 我正在尝试使用JSON_EXTRACT从我的MariaDB服务器获取JSON值。 However, there are some JSON keys having lot of white spaces like end of line, space, tabs etc. The data is already there. 但是,有些JSON键具有很多空白,例如行尾,空格,制表符等。数据已经存在。 Hence I am not able to give the correct key name because the key contains white spaces. 因此,我无法给出正确的键名称,因为键包含空格。 Please note that the white spaces are there in JSON values also, but we can use TRIM() function to remove the white spaces from values. 请注意,JSON值中也有空格,但是我们可以使用TRIM()函数从值中删除空格。 But what can we do to trim the key names? 但是,我们该怎么做才能修剪键名呢?

For example: 例如:

CREATE TABLE test.product_json_table (
   id INT AUTO_INCREMENT NOT NULL,
   product VARCHAR(20) NOT NULL,
   description LONGTEXT ASCII,
  PRIMARY KEY (id),
    CHECK (JSON_VALID(description))
) ENGINE = InnoDB ROW_FORMAT = DEFAULT;



INSERT INTO test.product_json_table(product, description) 
VALUES( 'truck_space', '{"     \r\nwheels  ": 4, "seats": 3, "  fuel   ": "diesel", "  \r\n mileage     ": 8}');

The below query does not work: 以下查询不起作用:

SELECT id, product, description 
FROM test.product_json_table
WHERE JSON_EXTRACT(description, '$.wheels') > 2;

The query does not work because there are white spaces be the JSON key "wheels". 该查询不起作用,因为JSON键“ wheels”有空格。 Same is the case with the key "mileage". 键“里程”也是如此。

How do we solve this issue? 我们如何解决这个问题? Thanks in advance. 提前致谢。

you can use 您可以使用

REGEXP_REPLACE(query, '\\s|\\r|\\n','')

see 看到

 CREATE TABLE product_json_table ( id INT AUTO_INCREMENT NOT NULL, product VARCHAR(20) NOT NULL, description LONGTEXT ASCII, PRIMARY KEY (id), CHECK (JSON_VALID(description)) ) 
\n \n
 INSERT INTO product_json_table(product, description) VALUES( 'truck_space', REGEXP_REPLACE('{" \\r\\nwheels ": 4, "seats": 3, " fuel ": "diesel", " \\r\\n mileage ": 8}', '\\\\s|\\\\r|\\\\n','')); 
\n \n
 select * from product_json_table 
\nid | id | product | 产品| description 描述                                       \n-: | -:| :---------- | :---------- | :------------------------------------------------- :-------------------------------------------------\n 1 | 1 | truck_space | truck_space | {"wheels":4,"seats":3,"fuel":"diesel","mileage":8} { “轮子”:4 “位”:3, “燃料”: “柴油”, “里程”:8}\n

db<>fiddle here db <> 在这里拨弄

In addition to @BillKarwin's suggestion to trim whitespace before you enter it into the database, you can also update all the values in the database to remove the spurious whitespace: 除了@BillKarwin建议在将空格输入数据库之前先修剪空格之外,还可以更新数据库中的所有值以删除虚假的空格:

UPDATE product_json_table
SET description = REGEXP_REPLACE(description, '\\s|\\r|\\n','');

Then your original query will work: 然后您的原始查询将起作用:

SELECT id, product, description 
FROM product_json_table
WHERE JSON_EXTRACT(description, '$.wheels') > 2;

Output: 输出:

id  product         description
1   truck_space     {"wheels":4,"seats":3,"fuel":"diesel","mileage":8}

Demo on dbfiddle dbfiddle上的演示

Update 更新

You can also perform the whitespace replacement on the fly although this will be much less efficient than permanently removing it with the UPDATE query above: 您也可以即时执行空白替换,尽管与使用上面的UPDATE查询永久删除它相比,效率要低得多:

SELECT id, product, REGEXP_REPLACE(description, '\\s|\\r|\\n','') AS description 
FROM product_json_table
WHERE JSON_EXTRACT(REGEXP_REPLACE(description, '\\s|\\r|\\n',''), '$.wheels') > 2

Output: 输出:

id  product         description
1   truck_space     {"wheels":4,"seats":3,"fuel":"diesel","mileage":8}

Demo on dbfiddle dbfiddle上的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM