bigfunctions > items2json
items2json¶
Call or Deploy items2json
?
✅ You can call this items2json
bigfunction directly from your Google Cloud Project (no install required).
- This
items2json
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
items2json(key_value_items)
Description
Returns json
object from array of key_value_items
which is a array<struct<key string, value string>>
.
Examples¶
select bigfunctions.eu.items2json([('a', 'foo'), ('b': 'bar')])
select bigfunctions.us.items2json([('a', 'foo'), ('b': 'bar')])
select bigfunctions.europe_west1.items2json([('a', 'foo'), ('b': 'bar')])
+--------------------------+
| json |
+--------------------------+
| {"a": "foo", "b": "bar"} |
+--------------------------+
2. ⚠️ Whatever the names of the struct fields: the first field is always considered as the key and the second as the value.
select bigfunctions.eu.items2json([struct('a' as value, 'foo' as key), struct('b' as one, 'bar' as two)])
select bigfunctions.us.items2json([struct('a' as value, 'foo' as key), struct('b' as one, 'bar' as two)])
select bigfunctions.europe_west1.items2json([struct('a' as value, 'foo' as key), struct('b' as one, 'bar' as two)])
+--------------------------+
| json |
+--------------------------+
| {"a": "foo", "b": "bar"} |
+--------------------------+
Need help using items2json
?
The community can help! Engage the conversation on Slack
For professional suppport, don't hesitate to chat with us.
Found a bug using items2json
?
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¶
Let's say you have a BigQuery table that stores product information, including a set of custom attributes for each product. These attributes are stored as key-value pairs in an array. You want to transform this array of key-value pairs into a JSON object for easier processing or export.
Example Table:
CREATE OR REPLACE TABLE `your_project.your_dataset.products` (
product_id INT64,
product_name STRING,
attributes ARRAY<STRUCT<key STRING, value STRING>>
);
INSERT INTO `your_project.your_dataset.products` (product_id, product_name, attributes) VALUES
(1, 'Product A', [('color', 'red'), ('size', 'large'), ('material', 'cotton')]),
(2, 'Product B', [('color', 'blue'), ('weight', '10kg')]);
Using items2json
:
You can use the items2json
function to convert the attributes
array into a JSON object:
SELECT
product_id,
product_name,
bigfunctions.your_region.items2json(attributes) AS attributes_json
FROM
`your_project.your_dataset.products`;
Result:
+------------+--------------+---------------------------------------------------+
| product_id | product_name | attributes_json |
+------------+--------------+---------------------------------------------------+
| 1 | Product A | {"color": "red", "size": "large", "material": "cotton"} |
| 2 | Product B | {"color": "blue", "weight": "10kg"} |
+------------+--------------+---------------------------------------------------+
Now, the attributes_json
column contains a JSON representation of the product attributes, making it easier to work with in downstream processes. For instance, you could easily extract individual attribute values using JSON functions or export the data in a JSON format.
Another use case could be dynamically constructing JSON payloads for API calls based on data stored in key-value pairs in BigQuery. Or you might use it to simplify the representation of complex data structures within BigQuery for analysis or reporting purposes.
Spread the word¶
BigFunctions is fully open-source. Help make it a success by spreading the word!