Skip to content

items2json

items2json(key_value_items)

Description

Returns json object from array of key_value_items which is a array<struct<key string, value string>>.

Usage

Call or Deploy items2json ?
Call items2json directly

The easiest way to use bigfunctions

  • items2json 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 items2json in your project

Why deploy?

  • You may prefer to deploy items2json 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

items2json function can be deployed with:

pip install bigfunctions
bigfun get items2json
bigfun deploy items2json

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"} |
+--------------------------+

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.


Need help or Found a bug?
Get help using items2json

The community can help! Engage the conversation on Slack

We also provide professional suppport.

Report a bug about 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.

We also provide professional suppport.


Show your ❤ by adding a ⭐ on