0

I have the following column in my database which is a postres jsonb object. I am just trying to fetch the value of additional_data from mytable which has the json string in it.

select additional_data from mytable;  
{"latitude":"123", "longitude":"234"}
{"latitude":"567", "longitude":"890"}

i'm just trying to get the value of latitude for my query and I am unable to figure out how the exact syntax would need to be for this. I tried to do the following

select latitude->>'additional_data' from mytable;
select latitude->'additional_data' from mytable;
select latitude#>>'additional_data' from mytable;

but when I do this, I get the following error

SQL Error [42703]: ERROR: column "latitude" does not exist
Position: 8

but that column definitely exists. Not sure what I am doing wrong.

2
  • 1
    I think your fields are reversed -- try SELECT additional_data->>'latitude' FROM mytable;
    – richyen
    Commented Mar 31, 2021 at 23:22
  • That works. If you can write the same as an answer, I will accept it as the solution. Thanks!
    – p0tta
    Commented Mar 31, 2021 at 23:33

1 Answer 1

1

According to the PostgreSQL documentation, you will need to reference the column on the left side of the operator, and the JSON key on the right side of the operator:

select additional_data->>'latitude' from mytable;
select additional_data->'latitude' from mytable;
select additional_data#>>'latitude' from mytable;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.