1

I have the following schema in redshift (an id column and a json dictionary as a string)

id data
1 {'1': 'true', '2':'false'}
2 {'1': 'false', '3':'true'}

and I would like to transform it to:

id key value
1 '1' 'true'
1 '2' 'false'
2 '1' 'false'
2 '3' 'true'

What would be an appropriate postgresql query for this? I have tried using JSON_EXTRACT_PATH_TEXT but then I need to union together all the individual keys and I am dealing with hundreds of keys.

1 Answer 1

1

something like

select id,json_object_keys(t.data) as key,data->json_object_keys(t.data) as value from t;

http://sqlfiddle.com/#!15/781fd/10

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

1 Comment

Unfortunately redshift uses postgresql 8.1 so the json function from later postgresql versions don't work. The above gives me the following error "[42883] ERROR: function json_object_keys("unknown") does not exist"

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.