【问题标题】:Separate JSON Arrays in PostgreSQL在 PostgreSQL 中分离 JSON 数组
【发布时间】:2021-01-28 19:31:55
【问题描述】:

我已将 JSON 文件上传到 PostgreSQL 10,但我的输出如下:

{
  "type":"name",
  "properties":{
    "name":"urn:ogc:def:crs:OGC:1.3:CRS84"
  }
}
[
  {
    "type":"Feature",
    "geometry":{
      "type":"Polygon",
      "coordinates":[
        [
          [
            -69.63156338410452,
            18.423064855125194
          ],
          [
            -69.63158484940756,
            18.42306049063717
          ],
          [
            -69.6316055531327,
            18.42316231511968
          ],
          [
            -69.63158408782962,
            18.4231666796077
          ],
          [
            -69.63156338410452,
            18.423064855125194
          ]
        ]
      ]
    },
    "properties":{
      "id":579,
      "top":2087416.483723811,
      "left":-7751401.453397826,
      "right":-7751399.014997826,
      "bottom":2087404.291723811
    }
  },
  {
    "type":"Feature",
    "geometry":{
      "type":"Polygon",
      "coordinates":[
        [
          [
            -69.63158408782962,
            18.4231666796077
          ],
          [
            -69.6316055531327,
            18.42316231511968
          ],
          [
            -69.6316262568703,
            18.42326413966371
          ],
          [
            -69.63160479156727,
            18.423268504151732
          ],
          [
            -69.63158408782962,
            18.4231666796077
          ]
        ]
      ]
    },
    "properties":{
      "id":580,
      "top":2087404.291723811,
      "left":-7751401.453397826,
      "right":-7751399.014997826,
      "bottom":2087392.099723811
    }
  },
  {
    "type":"Feature",
    "geometry":{
      "type":"Polygon",
      "coordinates":[
        [
          [
            -69.63160479156727,
            18.423268504151732
          ],
          [
            -69.6316262568703,
            18.42326413966371
          ],
          [
            -69.63164696062047,
            18.423365964269255
          ],
          [
            -69.6316254953174,
            18.42337032875728
          ],
          [
            -69.63160479156727,
            18.423268504151732
          ]
        ]
      ]
    },
    "properties":{
      "id":581,
      "top":2087392.099723811,
      "left":-7751401.453397826,
      "right":-7751399.014997826,
      "bottom":2087379.907723811
    }
  },
  {
    "type":"Feature",
    "geometry":{
      "type":"Polygon",
      "coordinates":[
        [
          [
            -69.6316254953174,
            18.423370328757276
          ],
          [
            -69.63164696062047,
            18.423365964269255
          ],
          [
            -69.6316676643831,
            18.423467788936325
          ],
          [
            -69.63164619908007,
            18.423472153424346
          ],
          [
            -69.6316254953174,
            18.423370328757276
          ]
        ]
      ]
    },
    "properties":{
      "id":582,
      "top":2087379.9077238112,
      "left":-7751401.453397826,
      "right":-7751399.014997826,
      "bottom":2087367.7157238112
    }
  },
  {
    "type":"Feature",
    "geometry":{
      "type":"Polygon",
      "coordinates":[
        [
          [
            -69.63164619908007,
            18.423472153424346
          ],
          [
            -69.6316676643831,
            18.423467788936325
          ],
          [
            -69.63168836815828,
            18.423569613664906
          ],
          [
            -69.63166690285523,
            18.42357397815293
          ],
          [
            -69.63164619908007,
            18.423472153424346
          ]
        ]
      ]
    },
    "properties":{
      "id":583,
      "top":2087367.7157238112,
      "left":-7751401.453397826,
      "right":-7751399.014997826,
      "bottom":2087355.5237238111
    }
  },
  {
    "type":"Feature",
    "geometry":{
      "type":"Polygon",
      "coordinates":[
        [
          [
            -69.63166690285523,
            18.42357397815293
          ],
          [
            -69.63168836815828,
            18.423569613664906
          ],
          [
            -69.63170907194598,
            18.423671438455013
          ],
          [
            -69.63168760664291,
            18.423675802943034
          ],
          [
            -69.63166690285523,
            18.42357397815293
          ]
        ]
      ]
    },
    "properties":{
      "id":584,
      "top":2087355.5237238111,
      "left":-7751401.453397826,
      "right":-7751399.014997826,
      "bottom":2087343.331723811
    }
  },
  {
    "type":"Feature",
    "geometry":{
      "type":"Polygon",
      "coordinates":[
        [
          [
            -69.63158484940756,
            18.42306049063717
          ],
          [
            -69.63160631471062,
            18.423056126149138
          ],
          [
            -69.63162701843578,
            18.423157950631648
          ],
          [
            -69.6316055531327,
            18.42316231511968
          ],
          [
            -69.63158484940756,
            18.42306049063717
          ]
        ]
      ]
    },
    "properties":{
      "id":596,
      "top":2087416.483723811,
      "left":-7751399.014997827,
      "right":-7751396.576597827,
      "bottom":2087404.291723811
    }
  },
  {
    "type":"Feature",
    "geometry":{
      "type":"Polygon",
      "coordinates":[
        [
          [
            -69.6316055531327,
            18.42316231511968
          ],
          [
            -69.63162701843578,
            18.423157950631648
          ],
          [
            -69.63164772217337,
            18.423259775175687
          ],
          [
            -69.6316262568703,
            18.42326413966371
          ],
          [
            -69.6316055531327,
            18.42316231511968
          ]
        ]
      ]
    },
    "properties":{
      "id":597,
      "top":2087404.291723811,
      "left":-7751399.014997827,
      "right":-7751396.576597827,
      "bottom":2087392.099723811
    }
  },
  {
    "type":"Feature",
    "geometry":{
      "type":"Polygon",
      "coordinates":[
        [
          [
            -69.6316262568703,
            18.42326413966371
          ],
          [
            -69.63164772217337,
            18.423259775175687
          ],
          [
            -69.63166842592354,
            18.423361599781227
          ],
          [
            -69.63164696062047,
            18.423365964269255
          ],
          [
            -69.6316262568703,
            18.42326413966371
          ]
        ]
      ]
    },
    "properties":{
      "id":598,
      "top":2087392.099723811,
      "left":-7751399.014997827,
      "right":-7751396.576597827,
      "bottom":2087379.907723811
    }
  },
  {
    "type":"Feature",
    "geometry":{
      "type":"Polygon",
      "coordinates":[
        [
          [
            -69.63164696062047,
            18.423365964269255
          ],
          [
            -69.63166842592354,
            18.423361599781227
          ],
          [
            -69.6316891296862,
            18.423463424448297
          ],
          [
            -69.6316676643831,
            18.423467788936325
          ],
          [
            -69.63164696062047,
            18.423365964269255
          ]
        ]
      ]
    },
    "properties":{
      "id":599,
      "top":2087379.9077238112,
      "left":-7751399.014997827,
      "right":-7751396.576597827,
      "bottom":2087367.7157238112
    }
  },
  {
    "type":"Feature",
    "geometry":{
      "type":"Polygon",
      "coordinates":[
        [
          [
            -69.6316676643831,
            18.423467788936325
          ],
          [
            -69.6316891296862,
            18.423463424448297
          ],
          [
            -69.63170983346134,
            18.423565249176885
          ],
          [
            -69.63168836815828,
            18.423569613664906
          ],
          [
            -69.6316676643831,
            18.423467788936325
          ]
        ]
      ]
    },
    "properties":{
      "id":600,
      "top":2087367.7157238112,
      "left":-7751399.014997827,
      "right":-7751396.576597827,
      "bottom":2087355.5237238111
    }
  },
  {
    "type":"Feature",
    "geometry":{
      "type":"Polygon",
      "coordinates":[
        [
          [
            -69.63168836815828,
            18.423569613664906
          ],
          [
            -69.63170983346134,
            18.423565249176885
          ],
          [
            -69.63173053724903,
            18.423667073966993
          ],
          [
            -69.63170907194598,
            18.423671438455013
          ],
          [
            -69.63168836815828,
            18.423569613664906
          ]
        ]
      ]
    },
    "properties":{
      "id":601,
      "top":2087355.5237238111,
      "left":-7751399.014997827,
      "right":-7751396.576597827,
      "bottom":2087343.331723811
    }
  },
  {
    "type":"Feature",
    "geometry":{
      "type":"Polygon",
      "coordinates":[
        [
          [
            -69.63160631471062,
            18.423056126149138
          ],
          [
            -69.6316277800137,
            18.42305176166112
          ],
          [
            -69.63164848373884,
            18.423153586143624
          ],
          [
            -69.63162701843578,
            18.423157950631648
          ],
          [
            -69.63160631471062,
            18.423056126149138
          ]
        ]
      ]
    },
    "properties":{
      "id":613,
      "top":2087416.483723811,
      "left":-7751396.576597827,
      "right":-7751394.138197826,
      "bottom":2087404.291723811
    }
  }
]

此字段中包含许多多边形。我希望能够将它们分成每个多边形的不同行以及类型、名称和属性列。这在 PostgreSQL 中可以实现吗?

例如:

Type ID Properties Geometries
Feature 579 "left": -7751401.453397826,"top": 2087416.483723811, "right": -7751399.014997826, "bottom": 2087404.291723811 [-69.63156338410452, 18.423064855125194],[-69.63158484940756, 18.42306049063717],[-69.6316055531327, 18.42316231511968],[-69.63158408782962, 18.4231666796077],[-69.63156338410452, 18.423064855125194]

【问题讨论】:

  • 请不要使用图片来显示文字信息。将jsonb 数据复制并粘贴到您的问题中。然后人们就会有一些他们可以构建一个例子的东西。您想要的输出示例也会有所帮助。
  • @AdrianKlaver 现在再次格式化,我希望数据在 PostgreSQL 中显示。谁能帮我实现这个目标?

标签: arrays json postgresql


【解决方案1】:

上面的字符串中有两位json。首先,

"{"type": "name", "properties": {"name": "urn:ogc:def:crs:OGC:1.3:CRS84"}}"

然后是一个使用适当缩进看起来更好的数组indented(结果here):

[
  {
    "type": "Feature",
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [
            -69.63156338410452,
            18.423064855125194
          ],
          [
            -69.63158484940756,
            18.42306049063717
          ],
          [
            -69.6316055531327,
            18.42316231511968
          ],
          [
            -69.63158408782962,
            18.4231666796077
          ],
          [
            -69.63156338410452,
            18.423064855125194
          ]
        ]
      ]
    },
    "properties": {
      "id": 579,
      "top": 2087416.483723811,
      "left": -7751401.453397826,
      "right": -7751399.014997826,
      "bottom": 2087404.291723811
    }
  },

所以,我假设所有对象都具有相同的结构,类似于上面。为了方便,我们把 json 放到一个表中……

CREATE TABLE foo ( d jsonb );
INSERT INTO foo VALUES ('the above json data')

以下应该做你想做的,包括转换为 postgres 几何类型:

SELECT
typ,
BOX( POINT((prop->>'left')::FLOAT,  (prop->>'top')::FLOAT),
     POINT((prop->>'right')::FLOAT, (prop->>'bottom')::FLOAT) ) bbox,
     REPLACE(REPLACE((geom->'coordinates')->>0,'[','('),']',')')::POLYGON
FROM (
SELECT 
    elem->>'type'         AS typ,
    elem->'properties'    AS prop,
    elem->'geometry'      AS geom
FROM (
SELECT jsonb_array_elements(d) elem FROM foo
) f1
)f2;

但是,在 SQL 查询中处理这种 json 并不灵活,所以如果你有多种几何类型(不仅仅是多边形)或者 json 结构可以变化,最好以更合适的方式处理使用 SQL 之前的语言。

【讨论】:

  • 谢谢鲍勃!尝试了您的代码,但是当我尝试执行 ERROR: cannot extract elements from an object SQL state: 22023 时 PostgreSQL 抛出此错误
  • 是“无法从标量中提取元素”吗?
  • 当然,只要使用 (prop->>'id')::INTEGER ;你也可以使用 elem->'properties'->>'id'
  • 我从头开始又试了一次,效果很好!谢谢鲍勃!
  • 好!请注意这里列出的 postgres 中有大量 json 函数和运算符postgresql.org/docs/13/functions-json.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-24
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
相关资源
最近更新 更多