首页 > 解决方案 > 在 Tableau 中将 PostgreSQL 嵌套 JSON 转换为数值数组

问题描述

我有一个 PostgreSQL 数据库,其中包含一个test_table包含单个记录的表。第一列是简单的store_id,第二列meausurement是嵌套的 json。

store_id | measurement
----------------------
0        | {...}

列的格式measurement如下:

{
    'file_info': 'xxxx', 
    'data': {
        'contour_data': {
            'X': [-97.0, -97.0, -97.0, -97.0, -97.0, -97.0],
            'Y': [-43.0, -41.0, -39.0, -39.0, -38.0, -36.0]
        }
    }
}

我想在 Tableau 的散点图中绘制Yvs。X因此,我使用 Tableau 的 PostgreSQL 连接器成功连接了数据库。从这个页面我了解到,我必须使用自定义 SQL 查询从 json 对象中提取数据,因为 Tableau 不直接支持jsonPostgres 的数据类型。我已经在 Tableau 中尝试了以下自定义 SQL 查询:

select
    store_id as store_id,
    measurement#>>'{data, contour_data, X}' as contour_points_x,
    measurement#>>'{data, contour_data, Y}' as contour_points_y
from test_table

它成功地将两个数组提取到两个新列contour_points_xcontour_points_y. 但是,这两个新列都在 Tableau 类型string中,因此我不能将它们用作绘图的数据源。

如何调整自定义 SQL 查询以使数据数组可在 Tableau 散点图中绘制?

标签: databasepostgresqldata-sciencetableau-apipostgresql-json

解决方案


看起来您需要拆分列。检查此https://help.tableau.com/current/pro/desktop/en-us/split.htm

编辑 - 当您可以可靠地假设每个列表中点数的上限时,链接方法有效。此处描述了一种拆分任意大小列表的方法https://apogeeintegration.com/blog/apogee-busts-out-multi-value-cells-using-tableau-prep-builder


推荐阅读