2

having JSON column in table I can selected nested properties like...

SELECT '{"monday": 123}'::json->>'monday'; --> returns 123

...but if I want to select properties dynamically, it doesn't work

SELECT '{"monday": 123}'::json->>to_char(now() AT TIME ZONE 'Pacific/Yap', 'day'); --> returns null

Is is even possible to achieve?

Thank you!

1 Answer 1

2

You need to add the FM prefix to the day keyword. to_char(..., 'day') has a fixed size, meaning after monday there are trailing spaces. The FM prefix removes them:

Click: demo:db<>fiddle

SELECT '{"monday": 123}'::json ->> to_char(now() AT TIME ZONE 'Pacific/Yap', 'FMday')
Sign up to request clarification or add additional context in comments.

1 Comment

You probably also need a lower() around the whole expression

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.