简体   繁体   中英

Remove leading and trailing whitespaces in JSON keys

I am trying to get a JSON value from my MariaDB server using JSON_EXTRACT. However, there are some JSON keys having lot of white spaces like end of line, space, tabs etc. The data is already there. 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. 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". 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 |  product |  description                                        \n-: |  :---------- |  :------------------------------------------------- \n 1 |  truck_space |  {"wheels":4,"seats":3,"fuel":"diesel","mileage":8} \n

db<>fiddle here

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:

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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