Map JSON to columns and rows in PostgreSQL -


i'm trying map json data columns. need contained in data array. example:

{"data":     [      {"stamp":1348249585,"date":"2012-09-21 17:46","blur":"blurs/1.jpg","img":["imgs/1.jpg",[1600,1200]],"thumb":["thumbs/1.jpg",[150,113]]},      {"stamp":1375607177,"date":"2013-08-04 09:06","blur":"blurs/2.jpg","img":["imgs/2.jpg",[1600,1200]],"thumb":["thumbs/2.jpg",[150,113]]},      {"stamp":1376242046,"date":"2013-08-11 17:27","blur":"blurs/3.jpg","img":["imgs/3.jpg",[1600,1200]],"thumb":["thumbs/3.jpg",[150,113]]},      ... 

currently, using #>> operator dynamically generated condition:

1) calculate number of elements in data array

2) create varchar array condition match every "row"

3) process elements on individual rows.

my solution is:

select    json_row,   json_row#>>'{stamp}' stamp,   json_row#>>'{date}' date,     json_row#>>'{img,0}' img,     json_row#>>'{img,1,0}' img_width,       json_row#>>'{img,1,1}' img_height,         json_row#>>'{thumb,0}' thumb,   json_row#>>'{thumb,1,0}' thumb_width,     json_row#>>'{thumb,1,1}' thumb_height,   json_row#>>'{thumb,2,0}' th_x1,     json_row#>>'{thumb,2,1}' th_y1,       json_row#>>'{thumb,3,0}' th_x2,         json_row#>>'{thumb,3,1}' th_y2,           json_row#>>'{blur}'   (   select     (gjson#>>c.cond)::json json_row       gallery_json      cross join (       select ('{data,'|| generate_series(0,         (select json_array_length((gjson#>>'{data}')::json) gallery_json) - 1) || '}')::varchar[] cond) c   ) rd 

this works , can live it. but, given first exercise json in postgresql ask if there better way map similar json structure rows. think supposed use json_populate_recordset, did not succeed far.

sqlfiddle not work currently, sample data:

--drop table if exists gallery_json;  create table gallery_json(gjson json);  insert gallery_json (gjson)    select '{"data":[     {"stamp":1348249585,"date":"2012-09-21 17:46","blur":"blurs/1.jpg","img":["imgs/1.jpg",[1600,1200]],"thumb":["thumbs/1.jpg",[150,113]]},     {"stamp":1376659268,"date":"2013-08-16 13:21","blur":"blurs/7.jpg","img":["imgs/7.jpg",[1600,539]],"thumb":["thumbs/7.jpg",[267,112],[332,112],[32,0]]},     {"stamp":1376666907,"date":"2013-08-16 15:28","blur":"blurs/8.jpg","img":["imgs/8.jpg",[1600,1200]],"thumb":["thumbs/8.jpg",[150,113]]},     {"stamp":1379016669,"date":"2013-09-12 20:11","blur":"blurs/11.jpg","img":["imgs/11.jpg",[1600,590]],"thumb":["thumbs/11.jpg",[267,112],[304,112],[18,0]]},     {"stamp":1383304027,"date":"2013-11-01 11:07","blur":"blurs/17.jpg","img":["imgs/17.jpg",[1600,1200]],"thumb":["thumbs/17.jpg",[150,113]]}]     ,"blur":[600,336],"thumb":{"min":[150,112],"max":[267,200]}}'::json 

sql fiddle

with data (     select json_array_elements(gjson -> 'data') data     gallery_json ) select     (data -> 'stamp')::text::bigint stamp,     (data -> 'date')::text::timestamp date,     (data -> 'blur')::text blur,     (data -> 'img' -> 0)::text img,     (data -> 'img' -> 1 -> 0)::text::int img_width,     (data -> 'img' -> 1 -> 1)::text::int img_height,     (data -> 'thumb' -> 0)::text thumb,     (data -> 'thumb' -> 1 -> 0)::text::int thumb_width,     (data -> 'thumb' -> 1 -> 1)::text::int thumb_height,     (data -> 'thumb' -> 2 -> 0)::text::int th_x1,     (data -> 'thumb' -> 2 -> 1)::text::int th_y1,     (data -> 'thumb' -> 3 -> 0)::text::int th_x2,     (data -> 'thumb' -> 3 -> 1)::text::int th_y2 data 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -