Skip to content

json_schema

json_schema(data)

Description

Returns the schema of data (with data a json object) as [{path, type}] with path the path of the nested field and type among (string, numeric, bool, date, timestamp)

Usage

Call or Deploy json_schema ?
Call json_schema directly

The easiest way to use bigfunctions

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

Why deploy?

  • You may prefer to deploy json_schema 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_schema function can be deployed with:

pip install bigfunctions
bigfun get json_schema
bigfun deploy json_schema

Examples

select bigfunctions.eu.json_schema({"created_at": "2022-01-01", "user": {"name": "James", "friends": ["Jack", "Peter"]}})
select bigfunctions.us.json_schema({"created_at": "2022-01-01", "user": {"name": "James", "friends": ["Jack", "Peter"]}})
select bigfunctions.europe_west1.json_schema({"created_at": "2022-01-01", "user": {"name": "James", "friends": ["Jack", "Peter"]}})
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| schema                                                                                                                                                          |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| [
|   struct("created_at" as path, "date" as type),
|   struct("user.name" as path, "string" as type),
|   struct("user.friends" as path, "array" as type)
| ]
 |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+

Use cases

A use case for the json_schema function is to dynamically determine the schema of JSON data stored in a BigQuery table without prior knowledge of its structure. This can be particularly helpful in situations like:

  • Data ingestion from diverse sources: Imagine receiving JSON data from various APIs or partners where the structure might not be consistent or documented thoroughly. json_schema can be used to automatically analyze a sample of the incoming data and infer its schema. This information can then be used to create or validate table schemas, ensuring proper data loading.

  • Data exploration and analysis: When dealing with unfamiliar JSON data, json_schema helps quickly understand its structure and the types of information it contains. This is useful for exploratory data analysis and building queries without manually examining the JSON objects.

  • Schema evolution tracking: By periodically applying json_schema to incoming data, you can detect changes in the JSON structure over time. This allows you to adapt your processing pipelines or table schemas as needed, ensuring compatibility and avoiding errors.

  • Data validation: After inferring the schema, it can be used to validate subsequent JSON data against the expected structure. This can prevent malformed data from being ingested, ensuring data quality.

  • Automated documentation: The output of json_schema can be used to generate documentation for the JSON data, simplifying communication and understanding among different teams or users.

Example Scenario:

Let's say you have a BigQuery table containing a raw_data column storing JSON strings from different sources. You can use the following query to get the schema of the JSON data in each row:

SELECT bigfunctions.us.json_schema(raw_data) AS inferred_schema
FROM your_dataset.your_table;

This will return a table where each row contains the inferred schema of the corresponding JSON data in raw_data. You can then further process this output to:

  • Identify the common schema across different JSON data.
  • Create a new table with the appropriate schema to store the extracted JSON data in a structured format.
  • Flag rows with unexpected schemas for further investigation.

By dynamically determining the schema of JSON data using json_schema, you can make your data ingestion, analysis, and validation processes more robust and efficient.


Need help or Found a bug?
Get help using json_schema

The community can help! Engage the conversation on Slack

We also provide professional suppport.

Report a bug about json_schema

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