1

I have a table with a "rules" column that looks like this:

{"111_abc":0,"222_def":0,"333_abs":0}

I'd like to write a postgres query to turn this into something like this:

rules
111_abc
222_def
333_abs

I have tried several renditions of json_extract_path or json_array_element - no luck.

2 Answers 2

1

Use the function jsonb_object_keys(), e.g.:

with my_table(rules) as (
values
    ('{"111_abc":0,"222_def":0,"333_abs":0}'::jsonb)
)

select jsonb_object_keys(rules) as rules
from my_table;

  rules  
---------
 111_abc
 222_def
 333_abs
(3 rows)
Sign up to request clarification or add additional context in comments.

1 Comment

awesome - thank you. I'm betting this works, but I just realized I'm pulling from a datawarehouse that uses postgres version 8.0.2. I don't think json data types were introduced until version 9.2. I'll either have to work on updating the sql version, or find a way to write a udf.
0
select * from json_each_text('{"111_abc":0,"222_def":0,"333_abs":0}')

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.