Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL - jsonb_each

Tags:

I have just started to play around with jsonb on postgres and finding examples hard to find online as it is a relatively new concept.I am trying to use jsonb_each_text to printout a table of keys and values but get a csv's in a single column.

I have the below json saved as as jsonb and using it to test my queries.

{   "lookup_id": "730fca0c-2984-4d5c-8fab-2a9aa2144534",   "service_type": "XXX",   "metadata": "sampledata2",   "matrix": [     {         "payment_selection": "type",         "offer_currencies": [             {               "currency_code": "EUR",               "value": 1220.42             }         ]     }   ] } 

I can gain access to offer_currencies array with

SELECT element -> 'offer_currencies' -> 0 FROM test t, jsonb_array_elements(t.json -> 'matrix') AS element WHERE element ->> 'payment_selection' = 'type' 

which gives a result of "{"value": 1220.42, "currency_code": "EUR"}", so if i run the below query I get (I have to change " for ')

select * from jsonb_each_text('{"value": 1220.42, "currency_code": "EUR"}')  Key            | Value ---------------|---------- "value"        | "1220.42" "currency_code"| "EUR" 

So using the above theory I created this query

SELECT jsonb_each_text(data) FROM (SELECT element -> 'offer_currencies' -> 0 AS data   FROM test t, jsonb_array_elements(t.json -> 'matrix') AS element   WHERE element ->> 'payment_selection' = 'type') AS dummy; 

But this prints csv's in one column

record --------------------- "(value,1220.42)" "(currency_code,EUR)" 
like image 252
Alan Mulligan Avatar asked Mar 04 '15 14:03

Alan Mulligan


People also ask

How do I query Jsonb data in PostgreSQL?

Querying the JSON documentPostgreSQL has two native operators -> and ->> to query JSON documents. The first operator -> returns a JSON object, while the operator ->> returns text. These operators work on both JSON as well as JSONB columns. There are additional operators available for JSONB columns.

How do I declare JSON in PostgreSQL?

To convert this PostgreSQL array into JSON, we can use the array_to_json function. 1select array_to_json(array_agg(lap)) 2from ( 3 select id, number, position, time, flag_type from laps 4) lap; 5 6[{"id":1, 7 "number":1, 8 "position":4, 9 "time":"628.744", 10 "flag_type":"Green"}, 11 ...]

What is PostgreSQL Jsonb?

JSONB stands for “JSON Binary” or “JSON better” depending on whom you ask. It is a decomposed binary format to store JSON. JSONB supports indexing the JSON data, and is very efficient at parsing and querying the JSON data. In most cases, when you work with JSON in PostgreSQL, you should be using JSONB.

Can we store JSON in PostgreSQL?

PostgreSQL offers two types for storing JSON data: json and jsonb . To implement efficient query mechanisms for these data types, PostgreSQL also provides the jsonpath data type described in Section 8.14. 7. The json and jsonb data types accept almost identical sets of values as input.


1 Answers

The primary problem here, is that you select the whole row as a column (PostgreSQL allows that). You can fix that with SELECT (jsonb_each_text(data)).* ....

But: don't SELECT set-returning functions, that can often lead to errors (or unexpected results). Instead, use f.ex. LATERAL joins/sub-queries:

select first_currency.* from   test t      , jsonb_array_elements(t.json -> 'matrix') element      , jsonb_each_text(element -> 'offer_currencies' -> 0) first_currency where  element ->> 'payment_selection' = 'type' 

Note: function calls in the FROM clause are implicit LATERAL joins (here: CROSS JOINs).

like image 101
pozs Avatar answered Sep 18 '22 14:09

pozs