2

I need to extract data from an array nested inside an object (let's say the column it lives in is metadata). It looks like the following:

{
    "name": "abc",
    "references": "abc",
    "owners": [
        "xyz", "abc"
    ],
    "groups": ["x", "x"]
}

Sometimes the metadata field could look like this:

{
    "name": "abc",
    "references": "abc",
    "owners": [
        "xyz", "abc"
    ],
    "groups": ["x"] -- one less value
}

Or this:

{
    "name": "abc",
    "references": "abc",
    "sources": "abc", -- new column
    "owners": [
        "xyz", "abc"
    ],
    "groups": ["x", "x"]
}

It would be really helpful if someone could give me some advice! Ideally I would unpivot the keys into their own columns and the values would be data within those columns, as rows. But I'm struggling to figure out how to parse it first

I've tried:

json_extract_path_text(metadata, 'groups') -- returns ["x", "x"]
JSON_EXTRACT_PATH_TEXT(JSON_EXTRACT_ARRAY_ELEMENT_TEXT(model_meta::VARCHAR, 0), 'groups'::VARCHAR) -- returns an error: JSON parsing error, 8001, nvalid json array object {}
2

1 Answer 1

2

You got your calls backwards:

JSON_EXTRACT_ARRAY_ELEMENT_TEXT(JSON_EXTRACT_PATH_TEXT((metadata, 'groups'), 0)

This code may give you ideas on a better way to do this:

with inputdata as (
    select json_parse('{"name": "abc", "references": "abc", "owners": ["xyz", "abc"], "groups": ["x", "y"]}') as metadata
)
select i.metadata.name, i.metadata.references, o, g
from inputdata i, i.metadata.owners o, i.metadata.groups g
2
  • thank you! this worked for me, JSON_EXTRACT_ARRAY_ELEMENT_TEXT(JSON_EXTRACT_PATH_TEXT(metadata, 'groups'), 0), but is there a way to dynamically get all values instead of specifying a place in the list? like instead of stating 0 or 1, have it loop through the list and output everything? Commented Jun 26 at 17:55
  • That's what my second code snippet does. It extract every value combination of both arrays into its own row. That's why this is a better way. Commented Jun 26 at 20:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.