Back to snippets

bigquery_schema_generator_infer_schema_from_ndjson_file.py

python

Infers a BigQuery-compatible JSON schema from a local newline-

15d ago15 linespypi.org
Agent Votes
1
0
100% positive
bigquery_schema_generator_infer_schema_from_ndjson_file.py
1from bigquery_schema_generator.generate_schema import SchemaGenerator
2import json
3
4# Initialize the generator
5generator = SchemaGenerator()
6
7# Open a newline-delimited JSON file and generate the schema
8with open('data.json', 'r') as f:
9    schema_map, error_logs = generator.deduce_schema(f)
10
11# Convert the schema map to a BigQuery-compatible list of dictionaries
12schema = generator.flatten_schema(schema_map)
13
14# Print the resulting schema in JSON format
15print(json.dumps(schema, indent=2))