You cannot do this directly with BigQuery, but you can make it work in two passes:
(1) Import the JSON data as a CSV file with one row column.
(2) Convert each row to pack a “any type” field into a string. Write a UDF that takes a row and emits the final set of columns that you would like. Add the result of this query to the target table.
Example
I will start with JSON:
{"a": 0, "b": "zero", "c": { "woodchuck": "a"}} {"a": 1, "b": "one", "c": { "chipmunk": "b"}} {"a": 2, "b": "two", "c": { "squirrel": "c"}} {"a": 3, "b": "three", "c": { "chinchilla": "d"}} {"a": 4, "b": "four", "c": { "capybara": "e"}} {"a": 5, "b": "five", "c": { "housemouse": "f"}} {"a": 6, "b": "six", "c": { "molerat": "g"}} {"a": 7, "b": "seven", "c": { "marmot": "h"}} {"a": 8, "b": "eight", "c": { "badger": "i"}}
Import it into BigQuery as a CSV with one STRING column (I called it "blob"). I had to set the delimiter character to something arbitrary and unlikely (thorn - 'þ'), or it turned off by default ','.
Verify that the table is imported correctly. You should see your simple single-column layout, and the look should look just like your source file.
Then we write a request to convert it to the desired form. In this example, we need the following scheme:
a (INTEGER) b (STRING) c (STRING -- packed JSON)
We can do this with UDF:
// Map a JSON string column ('blob') => { a (integer), b (string), c (json-string) } bigquery.defineFunction( 'extractAndRepack', // Name of the function exported to SQL ['blob'], // Names of input columns [{'name': 'a', 'type': 'integer'}, // Output schema {'name': 'b', 'type': 'string'}, {'name': 'c', 'type': 'string'}], function (row, emit) { var parsed = JSON.parse(row.blob); var repacked = JSON.stringify(parsed.c); emit({a: parsed.a, b: parsed.b, c: repacked}); } );
And the corresponding request:
SELECT a, b, c FROM extractAndRepack(JsonAnyKey.raw)
Now you just need to run the query (by selecting the desired target table), and you will have your data in the form that you like.
Row abc 1 0 zero {"woodchuck":"a"} 2 1 one {"chipmunk":"b"} 3 2 two {"squirrel":"c"} 4 3 three {"chinchilla":"d"} 5 4 four {"capybara":"e"} 6 5 five {"housemouse":"f"} 7 6 six {"molerat":"g"} 8 7 seven {"marmot":"h"} 9 8 eight {"badger":"i"}