0

I have following json array

{ "21553287" : [20304602], "2971244" : [20304602,21231942,21232245], "22400866" : [20304602], "22511397" : [20304602], "1800479" : [20304602], "22152979" : [20304602,21231681,21232245], "22471714" : [20304602,21231702], "22354302" : [20304602], "22363993" : [20304602], "22360635" : [20304602,21231679], "21987748" : [20304602,21379642], "21983181" : [20304602]}

to create this array i used

folders AS (
SELECT json_object_agg("Face":: text, r)  FROM (
SELECT array_agg("Folder") r, "Face" FROM "FaceFolder"

WHERE "Type" = 7 AND "Face" IS NOT NULL AND  "Folder" IS NOT NULL
    GROUP BY "Face") t

)

When tried to

SELECT "@Folder" WHERE "@Folder" ANY (((TABLE folders) -> (ca."@Face"::text))::text :: int[])

I got following error

ERROR: malformed array literal: "[20304602,20407231]"

i understand that i need to make json array to postgres array but don't know how

8
  • Postgres array syntax looks like, {20304602,20407231} Commented Jun 11, 2020 at 10:21
  • What are you trying to do? A description of your purpose, along with sample data and desired results, would be helpful here. Commented Jun 11, 2020 at 10:24
  • @AvinKavish It's actually not, it's Pycharm standart output for postgresql Commented Jun 11, 2020 at 10:40
  • @GMB i wanna unnest json array to postgres array and find math in 'ca' table ( the contexts of this table is not important). I search and found jsonb_array_elements_text function that could help, but anyway i don't know how to implement it since i always haw different key in my json array Commented Jun 11, 2020 at 10:44
  • What language is your SELECT statement in? It appears similar to SQL, but clearly isn't. Commented Jun 11, 2020 at 11:50

1 Answer 1

1

One option would be to store it as the text representation of int[] in the first place, then the cast back to int[] would work.

... json_object_agg("Face":: text, r::text) ...

Of course that makes for pretty weird JSON data if you aren't using the JSON structures throughout it.

Another option is to create a helper function to do the conversion:

create function json_to_intarray(json) returns int[] immutable parallel safe language SQL as $$ 
    select array_agg(x::int) from json_array_elements_text($1) f(x)
$$;
1
  • Thank you for your answer! Is this dialect will work with postgresql? Commented Jun 11, 2020 at 14:17

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.