0

I have a field that is of type jsonb in a PostgreSQL database.
Example:

{"timestamp":"2016-12-14T04:15:04.836Z","receiptResult":{"status":"successful","timestamp":"2016-12-14T04:15:04.739Z","notes":"Customer Accepted"}}

How can I only return the "notes" in a select statement, I've tried:

SELECT data::json->>'notes' as notes

But nothing is returned, if I use:

SELECT data::json->'receiptResult' as notes;

It returns:

{"status":"successful","timestamp":"2016-114T04:15:04.739Z","notes":"Customer Accepted"}

But I only need the text after "notes".

1 Answer 1

1

Inside key receiptResult has another JSON object, you cannot access it in top-level. Try this:

WITH sample AS (
    SELECT '{"timestamp":"2016-12-14T04:15:04.836Z","receiptResult":{"status":"successful","timestamp":"2016-12-14T04:15:04.739Z","notes":"Customer Accepted"}}'::jsonb AS my_column
)
SELECT my_column->'receiptResult'->>'notes' FROM sample;

As you can see, -> operator returns value as a JSONB and ->> operator returns as a text.

More info here.

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

3 Comments

Thanks for the answer Michel, out of the office today, will test as soon as possible.
No problem. Regards!
Glad to help you @RuanLabuschagne

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.