postgres json: How to query map keys to get children

From: Hector Menchaca <hm34306(at)hotmail(dot)com>
To: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: postgres json: How to query map keys to get children
Date: 2014-08-23 18:02:37
Message-ID: BAY178-W513E01F52C8A4324953A74F8D10@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 TechnicalSomething 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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message David G Johnston 2014-08-23 20:18:52 Re: postgres json: How to query map keys to get children
Previous Message Vik Fearing 2014-08-23 12:15:46 Re: Retrieve most recent 1 record from joined table