Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "name": "Id",
- "type": "INTEGER",
- "mode": "nullable"
- },
- {
- "name": "Address",
- "type": "RECORD",
- "mode": "repeated",
- "fields":[
- {
- "name": "Street",
- "type": "STRING",
- "mode": "nullable"
- },
- {
- "name": "City",
- "type": "STRING",
- "mode": "nullable"
- }
- ]
- }
- table_schema = bigquery.TableSchema()
- Id_schema = bigquery.TableFieldSchema()
- Id_schema.name = 'Id'
- Id_schema.type = 'INTEGER'
- Id_schema.mode = 'nullable'
- table_schema.fields.append(Id_schema)
- Address_schema = bigquery.TableFieldSchema()
- Address_schema.name = 'Address'
- Address_schema.type = 'RECORD'
- Address_schema.mode = 'repeated'
- Street_schema = bigquery.TableFieldSchema()
- Street_schema.name = 'Street'
- Street_schema.type = 'STRING'
- Street_schema.mode = 'nullable'
- Address_schema.fields.append(Street_schema)
- table_schema.fields.append(Address_schema)
- City_schema = bigquery.TableFieldSchema()
- City_schema.name = 'City'
- City_schema.type = 'STRING'
- City_schema.mode = 'nullable'
- Address_schema.fields.append(City_schema)
- table_schema.fields.append(Address_schema)
- {"Id": 1, "Address": {"Street":"MG Road","City":"Pune"}}
- {"Id": 2, "Address": {"City":"Mumbai"}}
- {"Id": 3, "Address": {"Street":"XYZ Road"}}
- {"Id": 4}
- {"Id": 5, "PhoneNumber": 12345678, "Address": {"Street":"ABCD Road", "City":"Bangalore"}}
- #standardSQL
- WITH t AS (
- SELECT '{"Id": 1, "Address": {"Street":"MG Road","City":"Pune"}}' line UNION ALL
- SELECT '{"Id": 2, "Address": {"City":"Mumbai"}}' UNION ALL
- SELECT '{"Id": 3, "Address": {"Street":"XYZ Road"}}' UNION ALL
- SELECT '{"Id": 4} ' UNION ALL
- SELECT '{"Id": 5, "PhoneNumber": 12345678, "Address": {"Street":"ABCD Road", "City":"Bangalore"}}'
- )
- SELECT
- JSON_EXTRACT_SCALAR(line, '$.Id') id,
- JSON_EXTRACT_SCALAR(line, '$.PhoneNumber') PhoneNumber,
- JSON_EXTRACT_SCALAR(line, '$[Address].Street') Street,
- JSON_EXTRACT_SCALAR(line, '$[Address].City') City
- FROM t
- Row id PhoneNumber Street City
- 1 1 null MG Road Pune
- 2 2 null null Mumbai
- 3 3 null XYZ Road null
- 4 4 null null null
- 5 5 12345678 ABCD Road Bangalore
Add Comment
Please, Sign In to add comment