Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在postgresql中将json数组转换为行_Sql_Arrays_Json_Postgresql - Fatal编程技术网

如何在postgresql中将json数组转换为行

如何在postgresql中将json数组转换为行,sql,arrays,json,postgresql,Sql,Arrays,Json,Postgresql,我试图转换这个结果,其中有一个JSON类型的列,它来自sql查询 { "rows": [ { "columns": { "jseq": 1, "Nombre": "0000956_LANZADOR", "rutaEsquema": "AXIS", "TipoDeComponente": "SQL", "value": 0, "detalleDelComponente": "So

我试图转换这个结果,其中有一个JSON类型的列,它来自sql查询

{
  "rows": [
    {
      "columns": {
        "jseq": 1,
        "Nombre": "0000956_LANZADOR",
        "rutaEsquema": "AXIS",
        "TipoDeComponente": "SQL",
        "value": 0,
        "detalleDelComponente": "Solución incidente 956"
      }
    },
    {
      "columns": {
        "jseq": 2,
        "Nombre": "0000956_02_Borrar_Mandatos.sql",
        "rutaEsquema": "AXIS",
        "TipoDeComponente": "SQL",
        "value": 1,
        "detalleDelComponente": "Brecha 67"
      }
    }
  ]
}
对此

Nombre                     | rutaEsquema | TipoDeComponente | detalleDelComponente
---------------------------+-------------+------------------+-----------------------
0000956_LANZADOR           | AXIS        | SQL              | Solución incidente 956
0000956_02_Borrar_Mandatos | AXIS        | SQL              | Brecha 67

我使用的是Postgresql
json\u to\u record
json\u to\u recordset
,从。就你而言:

SELECT cols.*
FROM json_to_recordset(yourJsonValue -> 'rows') AS rows(columns JSON),
     json_to_record(columns) AS cols(
       "Nombre" TEXT,
       "rutaEsquema" TEXT,
       "TipoDeComponente" TEXT,
       "detalleDelComponente" TEXT)

一个选项是使用
json\u each
函数将最外层的json对象扩展为一组键/值对,然后使用
json\u array\u elements
提取数组元素:

select elm->>'Nombre' as Nombre,
       elm->>'rutaEsquema' as rutaEsquema,
       elm->>'TipoDeComponente' as TipoDeComponente,
       elm->>'detalleDelComponente' as detalleDelComponente
  from
  (
   select elms -> 'columns' as elm 
     from(
          select json_array_elements(js.value) as elms
            from tab,
                 json_each(js_col) as js
         ) q1
  ) q2 

您看过Postgres中的JSON函数吗?“它来自sql查询”-您可以修改该查询吗?如果是,请发布-听起来没有必要创建一个JSON,只是为了再次对其进行分解。从查询返回的JSON来自一个由Jira插件生成的字段,该插件的结果总是JSON格式的,不是我返回了JSONAh OK,那么可能没问题。顺便说一下,请考虑您已找到有用的选项,您可以将嵌套查询简化为<代码>。
select elm->>'Nombre' as Nombre,
       elm->>'rutaEsquema' as rutaEsquema,
       elm->>'TipoDeComponente' as TipoDeComponente,
       elm->>'detalleDelComponente' as detalleDelComponente
  from
  (
   select elms -> 'columns' as elm 
     from(
          select json_array_elements(js.value) as elms
            from tab,
                 json_each(js_col) as js
         ) q1
  ) q2