I want to turn a JSON encoded list into a native BigQuery Array, ideally this would end up being a list of tuples or dictionaries with value, position entries. Hence the reference to the python enumerate functionality.
i.e.
[(idx, elem) for idx, elem in enumerate(json_list_string)]
[{'pos':idx, 'value':elem} for idx, elem in enumerate(json_list_string)]
The first part of turning the json into an array I already solved using this question
Edit:
WITH
my_ids AS (
SELECT 'xyz' as grp, '["7f9f98fh9g4ef393d3h5", "chg3g33f26949hg6067d", "g477e5973ec04g7c3232", "0de1ec83304d761he786", "3c1h1f153530g90g35c2", "946637g145h48322686f"]' as ids
UNION ALL
SELECT 'abc' as grp, '["7f9f98fh9g4ef393d3h5", "chg3g33fdsfsdfs49hg6067d", "g477e5973ec04g7c3232", "0de1ec83304d761he786", "3c1h1f153530g90g35c2", "946637g145h48322686f"]' as ids
)
SELECT
*
FROM my_ids
In an ideal world I would get an output like:
xyz, 7f9f98fh9g4ef393d3h5, 1
xyz, chg3g33f26949hg6067d, 2
...
abc, 946637g145h48322686f, 6
Please note, that the lists can be rather long (up to 24 entries and I kinda don't want to hardcode all the paths)
Edit2: (Possible Solution)
WITH
my_ids AS (
SELECT 'xyz' as grp, '["7f9f98fh9g4ef393d3h5", "chg3g33f26949hg6067d", "g477e5973ec04g7c3232", "0de1ec83304d761he786", "3c1h1f153530g90g35c2", "946637g145h48322686f"]' as ids
UNION ALL
SELECT 'abc' as grp, '["7f9f98fh9g4ef393d3h5", "chg3g33fdsfsdfs49hg6067d", "g477e5973ec04g7c3232", "0de1ec83304d761he786", "3c1h1f153530g90g35c2", "946637g145h48322686f"]' as ids
),
as_list AS (SELECT
*,
SPLIT(REGEXP_REPLACE(JSON_EXTRACT(ids,'$'), r'[\[\]\"]', ''), ',') AS split_items,
GENERATE_ARRAY(1, ARRAY_LENGTH(SPLIT(REGEXP_REPLACE(JSON_EXTRACT(ids,'$'), r'[\[\]\"]', ''), ','))) AS positions
FROM my_ids)
SELECT grp, ids, positions[OFFSET(off)] as pos
FROM as_list, unnest(split_items) as ids WITH OFFSET off