Query data inside an attribute array in a json column in Postgres 9.6

Ali*_*ice 6 sql postgresql json

I have a table say types, which had a JSON column, say location that looks like this:

{ "attribute":[
  {
    "type": "state",
    "value": "CA"
  },
  {
    "type": "distance",
    "value": "200.00"
  } ...
  ]
  } 
Run Code Online (Sandbox Code Playgroud)

Each row in the table has the data, and all have the "type": "state" in it. I want to just extract the value of "type": "state" from every row in the table, and put it in a new column. I checked out several questions on SO, like:

but could not get it working. I do not need to query on this. I need the value of this column. I apologize in advance if I missed something.

McN*_*ets 7

create table t(data json);
insert into t values('{"attribute":[{"type": "state","value": "CA"},{"type": "distance","value": "200.00"}]}'::json); 

select elem->>'value' as state
from t, json_array_elements(t.data->'attribute') elem
where elem->>'type' = 'state';
Run Code Online (Sandbox Code Playgroud)
| 状态|
| :---- |
| 加州 |

dbfiddle在这里