skip to Main Content

I have a field, called created_by in a bigquery table, with text like this:

   created by      
  "{"name": "Charles Darwin", "email": "[email protected]"}"

The field is a "STRING" type field.
What I need is to extract the name and the email in two sepparated field.
How can I achieve this?

Thanks

2

Answers


  1. You may consider and try below approach using BigQuery’s REGEX_EXTRACT function.

    with sample_data as (
      select "{"name": "Charles Darwin", "email": "[email protected]"}" as my_string
    )
    
    select REGEXP_EXTRACT(my_string, r'"name":(D+),') as ext_name,
    REGEXP_EXTRACT(my_string, r'"email":(.+)}') as ext_email
    from sample_data
    

    Sample Output:
    enter image description here

    My sample REGEX is the most basic one since I am not familiar with your complete data. You may enhance or create your own REGEX that can fit your needed validation based on your own analysis on your data.

    Login or Signup to reply.
  2. see below, link to google docs here:

    https://cloud.google.com/bigquery/docs/reference/standard-sql/json_functions#json_value

    select json_value(json "{"name": "Charles Darwin", "email": "[email protected]"}", ‘$.email’) as getEmail

    getemail

    select json_value(json "{"name": "Charles Darwin", "email": "[email protected]"}", ‘$.name’) as getName

    getName

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search