0

I have this very simple query which it returns an array of municipality identifiers, that separates them with a comma and has double quotes around each item. I want to be able to surround these double quotes with single quotes

select json_agg(ssn) from sde.muns

enter image description here

4
  • Can you give your expected output? Commented Feb 8, 2018 at 19:42
  • Doesn't that make it invalid JSON? Or do you want the result as a string? Commented Feb 8, 2018 at 19:44
  • just single quote around each item. result can be a string Commented Feb 8, 2018 at 19:53
  • That's not an array. It's a json object that contains a json array. Are you maybe looking for string_agg() instead? Commented Feb 8, 2018 at 19:59

1 Answer 1

1

lets say you have:

t=# with c(v) as (select * from generate_series(1020,1029))
select json_agg(v::text) from c;
                                     json_agg
----------------------------------------------------------------------------------
 ["1020", "1021", "1022", "1023", "1024", "1025", "1026", "1027", "1028", "1029"]
(1 row)

so you can:

t=# with c(v) as (select * from generate_series(1020,1029))
select translate(json_agg(v::text)::text,'"',$$'$$) from c;
                                    translate
----------------------------------------------------------------------------------
 ['1020', '1021', '1022', '1023', '1024', '1025', '1026', '1027', '1028', '1029']
(1 row)

which of course is not valid json any more. And I assumed you can have only numbers, so no accidental double quote to take care of

Sign up to request clarification or add additional context in comments.

Comments

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.