0

Let me explain . I have a json data set with values something like this:

"clientRequest": {
        "uri": "/flow.php?id=FS-6097&utm_source=facebook&utm_medium=cpc&utm_term=cold",
        "body": null,
        "bytes": 608,
        "flags": 1,     
        "referer": "http://m.facebook.com/",        
        "httpMethod": "GET",
        "httpProtocol": "HTTP/1.1"
    },

Now i want to create a Virtual column which only fetch value "FS-6097" from "clientRequest.uri". So a new column which just contains "FS-6097", not just whole uri.

I am already creating a virtual column for whole uri like below, which is fine.

ALTER TABLE `table_xyz_json`
    ADD `url` TEXT
    GENERATED ALWAYS AS (JSON_UNQUOTE(
        JSON_EXTRACT(json_data, '$.clientRequest.uri')
    ))
    VIRTUAL NULL;

any help would be highly appreciated .

1 Answer 1

1

One option is to use SUBSTRING_INDEX function:

ALTER TABLE `table_xyz_json`
  ADD `url` TEXT GENERATED ALWAYS
  AS (`json_data` ->> '$.clientRequest.uri') VIRTUAL NULL,
  ADD `id` VARCHAR(7) GENERATED ALWAYS
  AS (SUBSTRING_INDEX(
        SUBSTRING_INDEX(`url`,
        'id=', -1),
      '&', 1)
     ) VIRTUAL NULL;

See db-fiddle.

UPDATE

You should adjust the expression of the column generated according to all the business rules that apply. For example, if a rule is that id may not exist you can try something like:

ALTER TABLE `table_xyz_json`
  ADD `url` TEXT GENERATED ALWAYS
  AS (`json_data` ->> '$.clientRequest.uri') VIRTUAL NULL,
  ADD `id` VARCHAR(7) GENERATED ALWAYS
  AS (IF(`url` REGEXP 'id=',
         SUBSTRING_INDEX(
           SUBSTRING_INDEX(`url`,
           'id=', -1),
         '&', 1),
         NULL
        )
     ) VIRTUAL NULL;

See db-fiddle.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks this is great. What if i want some adjustment like. sometimes url contains "lp=XXXX" instead of "id=XXXX" . so i can make sure that column contains both of this
Hi, i applied it on my Dataset, and found that its not working as it should be. example for urls which don't have "id=" , like " /apple-touch-icon-precomposed.png " , it generates virtual col value as "apple" .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.