0

Can we write select statement to get schema of an existing table in JSON format or can we write bigquery to

Select statement should generate the json format in the result set. (or in bigquery - how to Select existing table schema as JSON format)

example - [ { "name": "year", "type": "INTEGER", "mode": "NULLABLE" }, { "name": "month", "type": "INTEGER", "mode": "NULLABLE" }, { "name": "project_bytes", "type": "BYTES", "mode": "NULLABLE" }, { "name": "lang", "type": "STRING", "mode": "NULLABLE" } ]

1 Answer 1

4

Metadata of datasets and tables and the columns can be accessed by a query. https://cloud.google.com/bigquery/docs/information-schema-intro

For yourdataset all columns of all tables can be show with their schema:

SELECT * FROM yourdataset.INFORMATION_SCHEMA.COLUMNS
where table_name="yourtablename"

Converting this into a JSON string is also possible:

Select table_name, TO_JSON_STRING(array_agg(schema_col)) schema
from (
SELECT
  table_name,column_name,
   STRUCT(T.column_name AS name,
    any_value(T.data_type) AS type,
    IF (any_Value(T.is_nullable)="YES","NULLABLE",null) AS mode
    ) as schema_col
FROM
  yourdataset.INFORMATION_SCHEMA.COLUMNS T
GROUP BY
  1,  2
) group by 1

query output

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.