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.
Run Code Online (Sandbox Code Playgroud)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';| 状态| | :---- | | 加州 |
dbfiddle在这里
| 归档时间: |
|
| 查看次数: |
5735 次 |
| 最近记录: |