postgres json: How to query map keys to get children - Mailing list pgsql-sql

From Hector Menchaca
Subject postgres json: How to query map keys to get children
Date
Msg-id BAY178-W513E01F52C8A4324953A74F8D10@phx.gbl
Whole thread Raw
Responses Re: postgres json: How to query map keys to get children  (David G Johnston <david.g.johnston@gmail.com>)
List pgsql-sql
I'm new to postgresql and I am having trouble finding an example of how to query the following:

    {
    "Skill": {
    "Technical": [
    { "Name": "C#",
     "Rating": 4,
     "Last Used": "2014-08-21"
    },
    { "Name": "ruby",
     "Rating": 4,
     "Last Used": "2014-08-21"
    }
    
    ],
    "Product": [
    { "Name": "MDM",
     "Rating": 4,
     "Last Used": "2014-08-21"
    },
    { "Name": "UDM",
     "Rating": 5,
     "Last Used": "2014-08-21"
    }
    ]
    }
    }

In short I struggling with understanding how to query through maps without having to be explicit about naming each key.

I have a query that does the following, though it seems a bit much to have to do...

    Select  'Technical' as SkillType
    , json_array_elements(ResourceDocument->'Skill'->'Technical')->>'Name' as SkillName
    , json_array_elements(ResourceDocument->'Skill'->'Technical')->>'Rating' as Rating
    , json_array_elements(ResourceDocument->'Skill'->'Technical')->>'Last Used' as LastUsed
    FROM testdepot.Resource
    
    UNION ALL
   
    Select 'Product' as SkillType
    , json_array_elements(ResourceDocument->'Skill'->'Product')->>'Name' as SkillName
    , json_array_elements(ResourceDocument->'Skill'->'Product')->>'Rating' as Rating
    , json_array_elements(ResourceDocument->'Skill'->'Product')->>'Last Used' as LastUsed
    FROM testdepot.Resource

I am trying to find a way to do this in 1 query that allows containing all keys of a map.
In this case Product and Technical
Something like:

    Select 'Product' as SkillType
    , json_array_elements(ResourceDocument->'Skill'->*)->>'Name' as SkillName
    , json_array_elements(ResourceDocument->'Skill'->*)->>'Rating' as Rating
    , json_array_elements(ResourceDocument->'Skill'->*)->>'Last Used' as LastUsed
    FROM testdepot.Resource




pgsql-sql by date:

Previous
From: Vik Fearing
Date:
Subject: Re: Retrieve most recent 1 record from joined table
Next
From: David G Johnston
Date:
Subject: Re: postgres json: How to query map keys to get children