1

I'm trying to extract an integer from a JSON blob stored in a text field in amazon redshift. My query looks (roughly) like:

select json_extract_path_text(json_column, 'integer_field')::int from data;

However, I'm getting a strange error:

ERROR: Invalid digit, Value '1', Pos 0, Type: Integer

Why is '1' an invalid digit type? What is happening here?

1
  • Note that I'm casting this to int because json_extract_path_text returns a string. Commented Sep 2, 2014 at 22:20

1 Answer 1

8

It turns out (thanks to this StackOverflow question) that when there is no 'integer_field' in the JSON blob, the value returned is the empty string ''. For some reason, that is not supported by the cast to integer function. Weirdly enough, a non-empty string ' ' is just fine as input, so the following workaround solved the problem:

nullif(json_extract_path_text(json_column, 'integer_field'), ' ')::int

And now we know.

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.