Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
选择作为JSON对象{key:{}_Json_Postgresql_Plpgsql - Fatal编程技术网

选择作为JSON对象{key:{}

选择作为JSON对象{key:{},json,postgresql,plpgsql,Json,Postgresql,Plpgsql,我的桌子: ID | something1 | something2 | ... 1 | meow | 5 | 2 | 4 | KITTIES | 是否有任何方法可以选择数据作为JSON格式,格式为{“1”:{“something1”:“meow”,“something2”:5},“2”:{…}?您可以使用它来获取数据库的API。然后,吃掉它!这是我能想象到的最快、最清晰的事情。如果您不介意在一行的JSON表示中重复ID字段,您可以执行

我的桌子:

ID | something1 | something2 | ...
1  | meow       | 5          |
2  | 4          | KITTIES    |

是否有任何方法可以选择数据作为JSON格式,格式为
{“1”:{“something1”:“meow”,“something2”:5},“2”:{…}

您可以使用它来获取数据库的API。然后,吃掉它!这是我能想象到的最快、最清晰的事情。

如果您不介意在一行的JSON表示中重复ID字段,您可以执行以下操作:

SELECT 
  format('{%s}',
    string_agg(
      format(
        '%s:%s',
        to_json(ID::text),
        row_to_json(my_table)
      ), ','
    ), ''
  )::json as json_object
FROM my_table;
这将为您提供一个JSON对象,该对象包含表中每一行的子对象,由ID字段中的值进行键控

有关更多详细信息,请参阅