3

Using PostgreSQL 9.3, the json_array_elements function returns each string element in an array as json strings.

select value from json_array_elements('["a", "b"]');

value 
-------
"a"
"b"

I would like to convert these to regular Postgres TEXT values but I'm at a loss. I tried value::TEXT but they are still double quoted, i.e. json strings.

2 Answers 2

10

As simple as:

select value from json_array_elements_text('["a", "b"]');
Sign up to request clarification or add additional context in comments.

2 Comments

This would be the way to go IF one were using postgres 9.4+
The way to go would be to upgrade then {;-)
1

I think u want this.

select REPLACE(value::TEXT,'"','') from json_array_elements('["a", "b"]');

3 Comments

That was what I was hoping to avoid. Resorting to REPLACE seems like a workaround for doing something wrong in the first place. My use case is a one-shot schema migration script so this is ok. I'd still like to see a "cleaner" solution.
I guess as " is treat like a text so u will not be able to get data without replacing it.
I went with this since the clean and correct way was only added in postgres 9.4 in the form of the json_array_elements_text function.

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.