0

Similarly to this question about json_extract_path_text, when I run this query in Redshift, I would expect json_extract_array_element_text to remove the backslashes from the "\"b\"" value:

select
    j, 
    json_extract_array_element_text(j, 0) as a
from (select '["\\"b\\""]' as j);

Instead, it appears that the string value is extracted verbatim, and the results look like this:

["\"b\""]
\"b\"

Is this intentional? If yes, what would be the idiomatic way to remove the backslashes?

6
  • Does this answer your question? PostgreSQL unescape JSON string Commented Aug 14, 2022 at 9:51
  • I’m voting to close this question because the reported "problem" is that string escaping and parsing of literals is working exactly as expected. Commented Aug 14, 2022 at 12:11
  • @MikeOrganek The literal parsing does work, the literal '["\\"b\\""]' is parsed as the string ["\"b\""]. But a JSON parser should parse that as an array with a single element, the string "b", not as an array containing the string \"b\". There is nothing in the json_extract_array_element_text (or json_extract_path_text, for that matter) to suggest that they are not following the JSON standard. Commented Aug 30, 2022 at 18:56
  • For clarification, I would expect the second line of the output to be "b" not \"b\" Commented Sep 6, 2022 at 14:13
  • The output should be a length 3 string, not a length 5 string. Compare Javascript JSON.parse('["\\"b\\""]')[0].length (gives 3) and Redshift select length(json_extract_array_element_text('["\\"b\\""]', 0)) (gives 5) Commented Sep 6, 2022 at 14:40

0

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.