Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgres inverse of json_each

I don't have json text in a field. I have a row with columns "text","format","keyname","..." and I want to export it to json in the format '{"value of keyname column": {"text":"value of text","format":"value of format",... }}'. Is that possible in postgresql?

I have been looking at a postgis2geojson example and learned about the row_to_json function already. http://www.postgresonline.com/journal/archives/267-Creating-GeoJSON-Feature-Collections-with-JSON-and-PostGIS-functions.html

Furthermore I have been looking at the postgres json documentation: http://www.postgresql.org/docs/9.3/static/functions-json.html

Unfortunately I found no function that covers my need explicitly but json_each does what I want just in the opposite direction. Generally, I think the json functions in postgres are designed on the assumption that only the column names can be used as keys. Am I right? Is there a SQL hack I could use to come around this? Thanks for any help.

EDIT:

select '"'||keyname||'":"'||row_to_json((select r from(Select text, format,
    (select username from my.users where users.id = table.uid) as username,
    machinename ) as r ))||'"'
    from my.table where id = 1;
like image 623
lukas83 Avatar asked Nov 19 '13 10:11

lukas83


People also ask

What is the difference between JSON and Jsonb in Postgres?

The json data type stores an exact copy of the input text, which processing functions must reparse on each execution; while jsonb data is stored in a decomposed binary format that makes it slightly slower to input due to added conversion overhead, but significantly faster to process, since no reparsing is needed.

What is Json_build_object in PostgreSQL?

Converts a comma-separated argument list to a JSON object. The argument list consists of alternating keys and values.

How extract JSON data from PostgreSQL?

2) Querying PostgreSQL JSON Data To query data, you can use a SELECT statement like in any other SQL query. You can use the native PostgreSQL operators to query the data in PostgreSQL. The operator -> returns a JSON object field by key. The operator ->> returns a JSON object field by text.

What is Jsonb in Postgres?

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.


1 Answers

Since version 9.4 postgres has json_object_agg function, which is the inverse of json_each. You can read the documentation here.

If the data look like this:

> select * from example;
 text | format | keyname 
------+--------+---------
 foo  | bar    | a
 dead | beef   | b
(2 rows)

Then you can aggregate all of that into an object with keys a and b with the following query:

> select json_object_agg(keyname, json_build_object('text', text, 'format', format)) from example;
                                     json_object_agg                                      
------------------------------------------------------------------------------------------
 { "a" : {"text" : "foo", "format" : "bar"}, "b" : {"text" : "dead", "format" : "beef"} }
(1 row)
like image 129
Constantin S. Pan Avatar answered Sep 27 '22 17:09

Constantin S. Pan