0

I facing one issues about select the jsonb column value. I want to display the check in and out time data when the day is equal to Monday. I tried to use

SELECT name, dayp -> 'days' ->> 'Monday'
FROM  attendanceprofile, 
      jsonb_array_elements(daysprofile) as dayp;

cannot get the value.

The value inside the jsonb column is

[ {
        "days": {
            "Monday": {
                "checkin": "7:00",
                "checkout": "18:00"
            },
            "Tuesday": {
                "checkin": "5:00",
                "checkout": "16:00"
            }
        }
    } ]
7
  • Your JSONB value is an array, so you would need to access by element, but perhaps it isn't supposed to be an array at all. Commented Jan 14, 2021 at 13:16
  • Which Postgres version are you using? Commented Jan 14, 2021 at 13:21
  • Postgres Version 10 Commented Jan 14, 2021 at 13:25
  • Is it always the first element in the array you want to look at? What if your array has more elements? Commented Jan 14, 2021 at 13:26
  • No. Depand on today day for display checkin and checkout times Commented Jan 14, 2021 at 13:31

1 Answer 1

1

If you always have a single element (or you always want this from the first element), you can pick those values using the "path operator" #>>

SELECT name, 
       daysprofile #>> '{0,days,Monday,checkin}' as checkin,
       daysprofile #>> '{0,days,Monday,checkout}' as checkout
FROM  attendanceprofile 
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.