python操作json_JSON/JSONB操作符
在Python中,我们通常使用json模块来处理JSON数据,以下是一些常用的操作:

读取JSON文件
import json
with open('data.json', 'r') as f:
data = json.load(f)
写入JSON文件
import json
data = {"name": "John", "age": 30, "city": "New York"}
with open('data.json', 'w') as f:
json.dump(data, f)
将JSON字符串转换为Python对象
import json
json_string = '{"name": "John", "age": 30, "city": "New York"}'
data = json.loads(json_string)
将Python对象转换为JSON字符串
import json
data = {"name": "John", "age": 30, "city": "New York"}
json_string = json.dumps(data)
对于PostgreSQL数据库中的JSONB类型,我们可以使用以下操作:
插入JSONB数据
INSERT INTO table_name (jsonb_column) VALUES ('{"key": "value"}');
查询JSONB数据
SELECT jsonb_column>'key' FROM table_name;
更新JSONB数据
UPDATE table_name SET jsonb_column = jsonb_column || '{"new_key": "new_value"}';
删除JSONB数据
UPDATE table_name SET jsonb_column = jsonb_column 'key';
文章标题:python操作json_JSON/JSONB操作符
标题链接:http://www.jxjierui.cn/article/cdscgdp.html


咨询
建站咨询
