bigfunctions > json_items
json_items¶
Call or Deploy json_items
?
✅ You can call this json_items
bigfunction directly from your Google Cloud Project (no install required).
- This
json_items
function is deployed inbigfunctions
GCP project in 39 datasets for all of the 39 BigQuery regions. You need to use the dataset in the same region as your datasets (otherwise you may have a function not found error). - Function is public, so it can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
- You may prefer to deploy the BigFunction in your own project if you want 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). Discover the framework
Public BigFunctions Datasets:
Region | Dataset |
---|---|
eu |
bigfunctions.eu |
us |
bigfunctions.us |
europe-west1 |
bigfunctions.europe_west1 |
asia-east1 |
bigfunctions.asia_east1 |
... | ... |
Description¶
Signature
json_items(json_string)
Description
Extract key_value_items
from json_string
which has only flat (no nested) key-values.
Return key_value_items
as array< struct<key string, value string> >
Examples¶
select bigfunctions.eu.json_items('{"created_at": "2022-01-01", "user": "sidali"}')
select bigfunctions.us.json_items('{"created_at": "2022-01-01", "user": "sidali"}')
select bigfunctions.europe_west1.json_items('{"created_at": "2022-01-01", "user": "sidali"}')
+-----------------------------------------------------------------------------------------------------+
| key_value_items |
+-----------------------------------------------------------------------------------------------------+
| [
| struct("created_at" as key, "date" as value),
| struct("user" as key, "name" as value)
| ]
|
+-----------------------------------------------------------------------------------------------------+
Need help using json_items
?
The community can help! Engage the conversation on Slack
For professional suppport, don't hesitate to chat with us.
Found a bug using json_items
?
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.
For professional suppport, don't hesitate to chat with us.
Use cases¶
You have a table in BigQuery containing a column with JSON strings representing product details. Each JSON string has a flat structure (no nested objects). You want to extract the key-value pairs from these JSON strings and analyze them.
Example Table:
products
| product_id | details |
|------------|--------------------------------------------|
| 1 | '{"name": "Laptop", "price": "1200", "brand": "XYZ"}' |
| 2 | '{"name": "Mouse", "price": "25", "brand": "ABC"}' |
| 3 | '{"name": "Keyboard", "price": "75", "brand": "XYZ"}'|
Query using json_items
:
SELECT
product_id,
item.key,
item.value
FROM
`your-project.your_dataset.products`,
UNNEST(bigfunctions.your-region.json_items(details)) AS item;
Result:
| product_id | key | value |
|------------|--------|-------|
| 1 | name | Laptop|
| 1 | price | 1200 |
| 1 | brand | XYZ |
| 2 | name | Mouse |
| 2 | price | 25 |
| 2 | brand | ABC |
| 3 | name | Keyboard |
| 3 | price | 75 |
| 3 | brand | XYZ |
This allows you to easily query and analyze individual attributes of the products, such as finding all products of a certain brand or within a specific price range. You've effectively transformed semi-structured JSON data into a relational format for easier analysis with standard SQL. Remember to replace your-project
, your_dataset
, and your-region
with your actual values.
Spread the word¶
BigFunctions is fully open-source. Help make it a success by spreading the word!