1

I have table users in a postgresql DB, The table contains a column settings of type jsonb. and here it is the json format:

{
  "device": {
    "352fef5aa349d63c": {
      "fcm": "Rg_4rdTaPwifTh-sP8gtRdI7VdMO_sShhuYbEpplVtmSfmIo8kkmqzIaFxfw59QXg3il95Y",
      "agent": "android",
      "language": "en",
      "app_version": 1
    },
    "3a922f2ead22ecb6": {
      "fcm": "MkqSrdTkPwiU32-sPKA_S8I7VdMO_tShhuYbEpplVtmSfmLo6kkmqzIaFxfw59QXg3il94X",
      "agent": "android",
      "language": "en",
      "app_version": 6
    }
  },
  "data": {
    "_email": "[email protected]",
    "_password": "grmbn9",
    "_username": "username",
    "_member_id": 57076
  },
  "email_status": 2,
  "email_verify_code": 9579
}

and I have to write a postgres function to return all devices FCMs in array. and here it is my function.

CREATE OR REPLACE FUNCTION GetUserFCM(userId int)
RETURNS TEXT[] 
AS $$
    DECLARE user_devices jsonb;
    DECLARE result TEXT[];
    DECLARE fcm TEXT[];
    DECLARE tmp TEXT;
BEGIN
    SELECT setting->'device' into user_devices FROM public."user" WHERE id = userId;
    SELECT ARRAY(SELECT jsonb_object_keys((SELECT setting->'device' FROM public."user" WHERE id = userId)::jsonb)) into result;

    FOR i IN 1 .. array_upper(result, 1)
    LOOP
        tmp := user_devices->i->'fcm';
        IF tmp IS NULL THEN
            PERFORM array_append(fcm, tmp);
        END IF;
    END LOOP;

    RETURN fcm;
END
$$
LANGUAGE plpgsql;

and when I execute

SELECT GetUserFCM(33) as result;

it returns nothing. any help how could I retrieve the devices FCMs from the json object please. and is there any other way better to retrieve the FCMs ?

2
  • what's your postgres version?.. Commented Oct 14, 2017 at 17:35
  • @VaoTsun I am using postgresql-9.4. Commented Oct 14, 2017 at 17:49

1 Answer 1

1

I would just select it:

t=> with j(b) as (values('{
  "device": {
    "352fef5aa349d63c": {
      "fcm": "Rg_4rdTaPwifTh-sP8gtRdI7VdMO_sShhuYbEpplVtmSfmIo8kkmqzIaFxfw59QXg3il95Y",
      "agent": "android",
      "language": "en",
      "app_version": 1
    },
    "3a922f2ead22ecb6": {
      "fcm": "MkqSrdTkPwiU32-sPKA_S8I7VdMO_tShhuYbEpplVtmSfmLo6kkmqzIaFxfw59QXg3il94X",
      "agent": "android",
      "language": "en",
      "app_version": 6
    }
  },
  "data": {
    "_email": "[email protected]",
    "_password": "grmbn9",
    "_username": "username",
    "_member_id": 57076
  },
  "email_status": 2,
  "email_verify_code": 9579
}'::jsonb)
)
, parse as (select b->'device'->jsonb_object_keys(b->'device')->>'fcm' jb from j)
select array_agg(jb) from parse;
                                                                     array_agg
---------------------------------------------------------------------------------------------------------------------------------------------------
 {Rg_4rdTaPwifTh-sP8gtRdI7VdMO_sShhuYbEpplVtmSfmIo8kkmqzIaFxfw59QXg3il95Y,MkqSrdTkPwiU32-sPKA_S8I7VdMO_tShhuYbEpplVtmSfmLo6kkmqzIaFxfw59QXg3il94X}
(1 row)
Sign up to request clarification or add additional context in comments.

2 Comments

And how could I insert this in a postgresql function to return the result in Array? I'm new in postgres.
the select in answer returns Array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.