N
N
Never Ever2019-10-18 13:14:40
PostgreSQL
Never Ever, 2019-10-18 13:14:40

PostgreSQL how to count number of keys in JSON?

I've been struggling with this problem for several hours now.
Let's say there is a table id-PK, data (json), any data can enter json, for example

   { 
      "name":"Vasya"
   },
   { 
      "name":"Petya",
      "secondName":"Petrov"
   },
   { 
      "name":"Serg",
      "secondName":"Sergs",
      "money":"42"
   }

In the end, how it should be
value  		| count
name  		  3
secondName     2
money  	         1

tried jsonb_object_keys(data)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2019-10-18
@Target1

select k, count(*) from tablename join  jsonb_object_keys(data) as k on true group by k

?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question