Guest User

Untitled

a guest
Feb 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. {
  2. "name": "Id",
  3. "type": "INTEGER",
  4. "mode": "nullable"
  5. },
  6. {
  7. "name": "Address",
  8. "type": "RECORD",
  9. "mode": "repeated",
  10. "fields":[
  11. {
  12. "name": "Street",
  13. "type": "STRING",
  14. "mode": "nullable"
  15. },
  16. {
  17. "name": "City",
  18. "type": "STRING",
  19. "mode": "nullable"
  20. }
  21. ]
  22. }
  23.  
  24. table_schema = bigquery.TableSchema()
  25.  
  26. Id_schema = bigquery.TableFieldSchema()
  27. Id_schema.name = 'Id'
  28. Id_schema.type = 'INTEGER'
  29. Id_schema.mode = 'nullable'
  30. table_schema.fields.append(Id_schema)
  31.  
  32. Address_schema = bigquery.TableFieldSchema()
  33. Address_schema.name = 'Address'
  34. Address_schema.type = 'RECORD'
  35. Address_schema.mode = 'repeated'
  36.  
  37. Street_schema = bigquery.TableFieldSchema()
  38. Street_schema.name = 'Street'
  39. Street_schema.type = 'STRING'
  40. Street_schema.mode = 'nullable'
  41. Address_schema.fields.append(Street_schema)
  42. table_schema.fields.append(Address_schema)
  43.  
  44. City_schema = bigquery.TableFieldSchema()
  45. City_schema.name = 'City'
  46. City_schema.type = 'STRING'
  47. City_schema.mode = 'nullable'
  48. Address_schema.fields.append(City_schema)
  49. table_schema.fields.append(Address_schema)
  50.  
  51. {"Id": 1, "Address": {"Street":"MG Road","City":"Pune"}}
  52. {"Id": 2, "Address": {"City":"Mumbai"}}
  53. {"Id": 3, "Address": {"Street":"XYZ Road"}}
  54. {"Id": 4}
  55. {"Id": 5, "PhoneNumber": 12345678, "Address": {"Street":"ABCD Road", "City":"Bangalore"}}
  56.  
  57. #standardSQL
  58. WITH t AS (
  59. SELECT '{"Id": 1, "Address": {"Street":"MG Road","City":"Pune"}}' line UNION ALL
  60. SELECT '{"Id": 2, "Address": {"City":"Mumbai"}}' UNION ALL
  61. SELECT '{"Id": 3, "Address": {"Street":"XYZ Road"}}' UNION ALL
  62. SELECT '{"Id": 4} ' UNION ALL
  63. SELECT '{"Id": 5, "PhoneNumber": 12345678, "Address": {"Street":"ABCD Road", "City":"Bangalore"}}'
  64. )
  65. SELECT
  66. JSON_EXTRACT_SCALAR(line, '$.Id') id,
  67. JSON_EXTRACT_SCALAR(line, '$.PhoneNumber') PhoneNumber,
  68. JSON_EXTRACT_SCALAR(line, '$[Address].Street') Street,
  69. JSON_EXTRACT_SCALAR(line, '$[Address].City') City
  70. FROM t
  71.  
  72. Row id PhoneNumber Street City
  73. 1 1 null MG Road Pune
  74. 2 2 null null Mumbai
  75. 3 3 null XYZ Road null
  76. 4 4 null null null
  77. 5 5 12345678 ABCD Road Bangalore
Add Comment
Please, Sign In to add comment