标签: json

如何将 JSON 数组转换为 Postgres 数组?

我有一个data类型的列,json其中包含这样的 JSON 文档:

{
    "name": "foo",
    "tags": ["foo", "bar"]
}
Run Code Online (Sandbox Code Playgroud)

我想将嵌套tags数组转换为连接字符串 ( 'foo, bar')。array_to_string()从理论上讲,使用该函数很容易做到这一点。但是,此功能不接受json输入。所以我想知道如何将这个 JSON 数组变成 Postgres 数组(类型text[])?

postgresql array postgresql-9.3 json

97
推荐指数
4
解决办法
28万
查看次数

将 Postgres 表导出为 json

有没有办法将 postgres 表数据作为 json 导出到文件?我需要逐行输出,例如:

{'id':1,'name':'David'}
{'id':2,'name':'James'}
...
Run Code Online (Sandbox Code Playgroud)

编辑:postgres 版本:9.3.4

postgresql export dump postgresql-9.3 json

54
推荐指数
5
解决办法
8万
查看次数

是否可以在 SQLite 中存储和查询 JSON?

我需要将 JSON 对象存储在 SQLite 数据库中,然后对其进行复杂的查询。

我做了一个这样的表:

+--------------------------------------+
|document |  property | string | number|
+--------------------------------------+
|foo      |  "title"  | "test" |       | 
+--------------------------------------+
|foo      |  "id"     |        |  42   | 
+--------------------------------------+
|bar      |  "id"     |        |  43   | 
+--------------------------------------+
Run Code Online (Sandbox Code Playgroud)

对于两个对象

foo {"title": "test", "id": 42} 
bar {id: 43}
Run Code Online (Sandbox Code Playgroud)

但我不能做“AND”查询,比如:

SELECT DISTINCT id  FROM table WHERE title = "test" AND id = 42 
Run Code Online (Sandbox Code Playgroud)

如您所见,“WHERE”之后的部分完全是胡说八道,但我不知道如何创建一个可以做我想做的查询。

那么您认为有更好的方法来存储我的数据,还是有一种解决方法来执行“AND”查询?

当然,JSON 可以包含任何属性,因此我无法为每个属性创建一个包含列的表。

我正在使用 WebSQL,它是没有扩展的 SQLite。

我知道我的问题很具体,但你能帮我吗?

sqlite json

51
推荐指数
3
解决办法
10万
查看次数

选择 json_agg 中的列

我有一个查询,如:

SELECT a.id, a.name, json_agg(b.*) as "item"
  FROM a
  JOIN b ON b.item_id = a.id
 GROUP BY a.id, a.name;
Run Code Online (Sandbox Code Playgroud)

如何选择在列b,所以我没有b.item_id在JSON对象?

我读过关于ROW,但它返回一个 JSON 对象,如:

{"f1": "Foo", "f2": "Bar"}
Run Code Online (Sandbox Code Playgroud)

一旦获取它以匹配正确的列键,我将需要重新映射 JSON 对象。我想避免这种情况并保留原始列名。

postgresql group-by row json

41
推荐指数
4
解决办法
10万
查看次数

Postgres 多列到 json

我正在运行 postgresql 9.3.4。我有一个包含 3 个字段的表:

id   name  addr
---  ----  ----
1    n1    ad1
2    n2    ad2
...
Run Code Online (Sandbox Code Playgroud)

我需要将数据移动到包含以下字段的新表:

id   data
---  ----
1    {'name': 'n1', 'addr': 'ad1'}
2    {'name': 'n2', 'addr': 'ad2'}
...
Run Code Online (Sandbox Code Playgroud)

row_to_json对我来说不是解决方案,因为它也SELECT t.id, row_to_json(t) as data FROM (select id, name, addr from myt) t增加id了结果。有没有办法在我的数据字段中选择我需要的字段(名称和地址)?

postgresql row json

33
推荐指数
2
解决办法
5万
查看次数

针对多个值的 PostgreSQL JSON 查询数组

我想针对jsonbPostgres中的类型编写一个查询,给定一组客户 ID 将找到相应的组。

鉴于此示例表:

CREATE TABLE grp(d JSONB NOT NULL);

INSERT INTO grp VALUES
   ('{"name":"First","arr":["foo"], "customers":[{"id":"1", "name":"one"},{"id":"2", "name":"two"}]}')
 , ('{"name":"Second","arr":["foo","bar"], "customers":[{"id":"3", "name":"three"},{"id":"4", "name":"four"}]}')
 , ('{"name":"Third","arr":["bar","baz"], "customers":[{"id":"5", "name":"five"},{"id":"6", "name":"seven"}]}');
Run Code Online (Sandbox Code Playgroud)

我发现了类似的问题(针对多个值的 PostgreSql JSONB SELECT)并设法使用此查询在简单数组上实现了我想要的:

SELECT d FROM grp WHERE d->'arr' ?| ARRAY['foo', 'bar'];
Run Code Online (Sandbox Code Playgroud)

但是,当数组包含 JSON对象时,我无法使其工作:

SELECT d FROM grp WHERE d->'customers' ?| ARRAY['{"id":"1"}', '{"id":"5"}'];
Run Code Online (Sandbox Code Playgroud)

这是我对查询的期望:

grp“第一个”-> 客户“1”

grp "第三" -> 客户 "5"

postgresql array json postgresql-9.4

27
推荐指数
1
解决办法
5万
查看次数

postgresql:如何定义具有默认值的 JSONB 列

我无法在文档中找到如何JSONB在 PostgreSQL 中创建一个DEFAULT值为空 json 文档的列。

如何在CREATE TABLE定义中说明上述内容?

postgresql json

26
推荐指数
2
解决办法
3万
查看次数

使用 JSONB 加入 PostgreSQL

我有这个 SQL:

CREATE TABLE test(id SERIAL PRIMARY KEY, data JSONB);

INSERT INTO test(data) VALUES
   ('{"parent":null,"children":[2,3]}'),
   ('{"parent":1,   "children":[4,5]}'),
   ('{"parent":1,   "children":[]}'),
   ('{"parent":2,   "children":[]}'),
   ('{"parent":2,   "children":[]}');
Run Code Online (Sandbox Code Playgroud)

那会给:

 id |                 data                 
----+--------------------------------------
  1 | {"parent": null, "children": [2, 3]}
  2 | {"parent": 1, "children": [4, 5]}
  3 | {"parent": 1, "children": []}
  4 | {"parent": 2, "children": []}
  5 | {"parent": 2, "children": []}
Run Code Online (Sandbox Code Playgroud)

当进行正常的一对多时,它会显示如下内容:

SELECT * 
FROM test x1
  LEFT JOIN test x2
    ON x1.id = (x2.data->>'parent')::INT;
 id |                 data                 | id | …
Run Code Online (Sandbox Code Playgroud)

postgresql join json postgresql-9.4

24
推荐指数
1
解决办法
5万
查看次数

在 PostgreSQL 中查询 JSONB

我有一个表,persons它包含两列,一个id和一个基于 JSONB 的data列(这个表只是为了演示目的而制作的,以使用 PostgreSQL 的 JSON 支持)。

现在,假设它包含两条记录:

1, { name: 'John', age: 30 }
2, { name: 'Jane', age: 20 }
Run Code Online (Sandbox Code Playgroud)

现在,假设我想得到每个 25 岁以上的人的名字。我尝试过的是:

select data->'name' as name from persons where data->'age' > 25
Run Code Online (Sandbox Code Playgroud)

不幸的是,这会导致错误。我可以通过使用->>代替来解决它->,但是比较不再按预期工作,因为不是比较数字,而是比较它们作为字符串的表示:

select data->'name' as name from persons where data->>'age' > '25'
Run Code Online (Sandbox Code Playgroud)

然后我发现我实际上可以通过使用->和强制转换来解决这个问题int

select data->'name' as name from persons where cast(data->'age' as int) > 25
Run Code Online (Sandbox Code Playgroud)

这有效,但我必须知道实际类型并不是那么好(ageJSON 文档中的类型是number无论如何,那么为什么 PostgreSQL …

postgresql cast json operator postgresql-9.5

23
推荐指数
1
解决办法
3万
查看次数

查看 MySQL 中的 JSON 数组是否包含其键包含特定日期的对象

我试图找出在 JSON 数组中是否有一行包含特定日期

假设我的数据如下所示:

表应用:

id | application_id | data
Run Code Online (Sandbox Code Playgroud)
id | application_id | data
Run Code Online (Sandbox Code Playgroud)

如何找到数据包含日期的所有应用程序'2016-04-26'

所以基本上我可以这样做:

select id, json_extract(`data`, "$[*].date") from applications
Run Code Online (Sandbox Code Playgroud)

返回:

# Rows
1 | 1 | [{"data" : ["some", "data#1"], "date": "2016-04-21"}, {"data" : ["other", "data#1"], "date" : "2016-04-22"}]
2 | 2 | [{"data" : ["some", "data#2"], "date": "2016-04-21"}, {"data" : ["other", "data#2"], "date" : "2016-04-26"}]
3 | 1 | [{"data" : ["some", "data#3"], "date": "2016-04-22"}, {"data" : ["other", "data#3"], "date" : "2016-04-26"}]
4 …
Run Code Online (Sandbox Code Playgroud)

mysql json

23
推荐指数
1
解决办法
6万
查看次数