0

How we can split this in a single query, Any ideas or suggestions ?

{"_xxx8430yyy59_xx":{"label":"Campaign Codes"},"_zzz984ggg4110_zzz":{"label":"NL Discount Codes"},"_ttt9843hhh160_ttt":{"label":"OOP Discount Codes"},"_ddd984393lll3_ddd":{"label":"Influencer Codes (Offer)"} }

I have tried to do this in multiple statements , but was not successful.

WITH CTE AS(select *,  unnest(string_to_array(value, ',')) AS parts from config_data)
SELECT parts from CTE

Expected output as

ID                      label
_xxx8430yyy59_xx         Campaign Codes
_zzz984ggg4110_zzz        NL Discount Codes
_ttt9843hhh160_ttt        OOP Discount Codes
_ddd984393lll3_ddd        Influencer Codes (Offer)

1 Answer 1

2

You don't string-split anything, you use the JSON processing functions that are built into postgres:

SELECT key as "ID", obj.value->>'label' AS label
FROM config_data,
jsonb_each(config_data.value::jsonb) AS obj(key, value)

You should also change the data type of the value column in your table to be jsonb/json, if you haven't already.

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.