json - 如何从json转换字符串?
问题描述
我正在研究由 MongoDB 提供支持的 postgresql 基础。摄入量全部采用 JSON 格式,碰巧某些字段以 STRING 形式出现,我无法检索该值。
例如,字段和值如下:
"Info":
"{\" categoryName \ ": \" Sale \ ", \" seller \ ": \" Uber \ ", \" categoryId \ ": \" 022 \ ", \" Payment \ ": false}"
已经尝试使用 REPLACE、SPLIT_PART、CONVERTER STRING PRA JSON,但没有任何效果。
SELECT case
when json->'payload'->'com'->0->>'Info' is null
then ''
else json->'payload'->'com'->0->>'Info'::json
end Test
FROM tableJson
我需要将此行转换为 JSON:
"Info": {
"categoryName": "Sale",
"seller": "Uber",
"categoryId": "022",
"Payment": false "}
解决方案
您需要进行一些正则表达式替换以删除所有反斜杠,然后使用大括号格式化外部节点{}
:
postgres=# select cast('{"Info":
{"categoryName": "Sale", "seller": "Uber", "categoryId": "022", "Payment": false}}' as json);
json
------------------------------------------------------------------------------------
{"Info": +
{"categoryName": "Sale", "seller": "Uber", "categoryId": "022", "Payment": false}}
(1 row)
推荐阅读
- bash - 将下一行的长度添加到bash中的当前行
- javascript - JavaScript:如何组合两个不同但非常相似的函数?
- c++ - 使用静态库的自定义框架 - 在框架模块 Xcode 9 中包含非模块化标头
- bash - 如何修复 -bash: alias: vim: not found 错误?
- c# - DataTable 排序而不创建新的 DataTable 并维护 RowState
- javascript - jQuery在单击雪佛龙时折叠和展开表格行
- python - Google API 客户端,调用已构建的 API,参数名称中带有点
- java - 扩展 JBoss Data Virt ws 转换器以处理分页源
- directory - MUI_PAGE_DIRECTORY 默认目录
- php - Symfony 3、RESTAPI、PUT、文件上传