pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Hector Menchaca <hm34306@hotmail.com>
To: pgsql-sql@postgresql.org <pgsql-sql@postgresql.org>
Subject: postgres json: How to query map keys to get children
Date: Sat, 23 Aug 2014 13:02:37 -0500
Message-ID: <BAY178-W513E01F52C8A4324953A74F8D10@phx.gbl> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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



 		 	   		  =

view thread (3+ messages)  latest in thread

Message-ID: <BAY178-W513E01F52C8A4324953A74F8D10@phx.gbl>
Permalink:  ../BAY178-W513E01F52C8A4324953A74F8D10@phx.gbl/
Also on:    postgresql.org/message-id/BAY178-W513E01F52C8A4324953A74F8D10@phx.gbl

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: hm34306@hotmail.com
  Subject: Re: postgres json: How to query map keys to get children
  In-Reply-To: <BAY178-W513E01F52C8A4324953A74F8D10@phx.gbl>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox