How-to loop over JSON Arrays in postgresql 9.3

json, postgresql

Solution

I was a little dumb, but the documentation on this json feature on postgresql website is actually minimal

to solve the problem all i did was

DO
$BODY$
DECLARE
    omgjson json := '[{ "type": false }, { "type": "photo" }, {"type": "comment" }]';
    i json;
BEGIN
  FOR i IN SELECT * FROM json_array_elements(omgjson)
  LOOP
    RAISE NOTICE 'output from space %', i->>'type';
  END LOOP;
END;
$BODY$ language plpgsql

Problem

I'm writing function for a new postgreSQL db and i'm trying to loop over a nested structure. Is that even possible with the new JSON functions? What i'm trying to do is here below: ``` DO $BODY$ DECLARE omgjson json := '[{ "type": false }, { "type": "photo" }, {"type": "comment" }]'; i record; BEGIN FOR i IN SELECT * FROM json_array_elements(omgjson) LOOP RAISE NOTICE 'output from space %', i; END LOOP; END; $BODY$ language plpgsql ``` This returns a set of records (text!), that is not JSON! so i cannot query it like `i->>'type'`, but that's exactly what i want to accomplish...

Original source