Skip to content

How to properly UNNEST a JSON Array? #2933

@christophediprima

Description

@christophediprima

Hi there,

What is the proper way to UNNEST a JSON array? I have following JSON structure saved in a raw column of my table :

{
    "post_id": "1",
    "mentioned_users": [
        {
            "user_id": "B"
        },
        {
            "user_id": "C"
        }
    ]
}

I would like to convert it in a table like like so:

post_id mentioned_user_id
1 B
1 C

I have something like this that works:

MODEL (
  name mentions.full_model,
  kind FULL,
  cron '@daily',
  grain ('post_id', 'mentioned_user_id')
);

SELECT  
  raw.post_id AS `post_id`,
  mentioned_user.user_id as `mentioned_user_id`
FROM posts.seed_model AS raw, 
  UNNEST(CAST(JSON_EXTRACT(raw, '$.mentioned_users') AS ARRAY<STRUCT<user_id STRING>>)) AS mentioned_user

But the lineage is not working properly for the mentioned_user_id column. When I select it the column greys out and a new mentioned_user columns appear with the lineage on the UNNEST line. Is this the best I can get?

Metadata

Metadata

Assignees

Labels

Project: UIIssues related to creating the UI

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions