I have a table with the following structure.
ID | json
bla | [{"user": "[email protected]", "timestamp": 0, "status": 1}, {"user": "[email protected]", "timestamp": 1, "status": 2}];
etc.
Now I want to read them, so that I have the follwing structure in bigquery.
ID | USER | TIMESTAMP | STATUS
bla [email protected] 0 1
bla [email protected] 1 2
When doing this:
CREATE TEMPORARY FUNCTION CUSTOM_JSON_EXTRACT(json STRING, json_path STRING)
RETURNS ARRAY<STRING>
LANGUAGE js AS """
return jsonPath(JSON.parse(json), json_path);
"""
OPTIONS (
library="gs://json_path/jsonpath-0.8.0.js"
);
WITH t AS (
SELECT id, history AS json_column FROM TABLE WHERE history IS NOT NULL
)
SELECT
id,
CUSTOM_JSON_EXTRACT(json_column , '$[*].email') AS email,
CUSTOM_JSON_EXTRACT(json_column , '$[*].status') AS status,
CUSTOM_JSON_EXTRACT(json_column , '$[*].timestamp') AS timestamp
FROM t
I don't get this row by row, but I get 1 row with sub rows...
[{user: '[email protected]', timestamp: 0, status: 1}, {user: '[email protected]', timestamp: 1, status: 2}]is simply a invalid JSON structure -> jsonlint.com -> "Error: Parse error on line 1: [{ user: '[email protected]', tim ---^ Expecting 'STRING', '}', got 'undefined'" .. So don't expected JSON parsing functions to work correctly[{ "user": "[email protected]", "timestamp": 0, "status": 1 }, { "user": "[email protected]", "timestamp": 1, "status": 2 }]