Skip to content

json_values

json_values(json_string)

Description

Extract values from json_string which has only flat (no nested) key-values. Return values as an array<string>

Usage

Call or Deploy json_values ?
Call json_values directly

The easiest way to use bigfunctions

  • json_values function is deployed in 39 public datasets for all of the 39 BigQuery regions.
  • It can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • (You need to use the dataset in the same region as your datasets otherwise you may have a function not found error)

Public BigFunctions Datasets

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...
Deploy json_values in your project

Why deploy?

  • You may prefer to deploy json_values in your own project to build and manage your own catalog of functions.
  • This is particularly useful if you want to create private functions (for example calling your internal APIs).
  • Get started by reading the framework page

Deployment

json_values function can be deployed with:

pip install bigfunctions
bigfun get json_values
bigfun deploy json_values

Examples

select bigfunctions.eu.json_values("{\"created_at\": \"2022-01-01\", \"user\": \"sidali\"}")
select bigfunctions.us.json_values("{\"created_at\": \"2022-01-01\", \"user\": \"sidali\"}")
select bigfunctions.europe_west1.json_values("{\"created_at\": \"2022-01-01\", \"user\": \"sidali\"}")
+--------------------------+
| values                   |
+--------------------------+
| ['2022-01-01', 'sidali'] |
+--------------------------+

Use cases

You have a table in BigQuery that stores JSON strings representing user activity. Each JSON string contains key-value pairs where the keys represent activity types and the values represent timestamps or user IDs. You want to extract all the values from these JSON strings to analyze the different types of activities performed without needing to know the specific keys.

Example Table:

UserID ActivityJSON
1 {"login": "2023-10-26 10:00:00", "purchase": "item123"}
2 {"logout": "2023-10-26 10:15:00", "view_product": "item456"}
3 {"login": "2023-10-26 10:30:00", "add_to_cart": "item789"}

Query using json_values:

SELECT
    UserID,
    bigfunctions.us.json_values(ActivityJSON) AS ActivityValues
FROM
    `your_project.your_dataset.your_table`;

Result:

UserID ActivityValues
1 ['2023-10-26 10:00:00', 'item123']
2 ['2023-10-26 10:15:00', 'item456']
3 ['2023-10-26 10:30:00', 'item789']

Now you have an array of values for each user, which you can further process. For instance, you could unnest the array to analyze the frequency of different activity values or join it with another table based on these values. The key benefit here is that you've extracted the relevant data without needing to explicitly parse the JSON based on individual keys. This is particularly useful when the keys in the JSON strings can vary across different rows but the values themselves hold the information you're interested in.


Need help or Found a bug?
Get help using json_values

The community can help! Engage the conversation on Slack

We also provide professional suppport.

Report a bug about json_values

If the function does not work as expected, please

  • report a bug so that it can be improved.
  • or open the discussion with the community on Slack.

We also provide professional suppport.


Show your ❤ by adding a ⭐ on