Title: How do I access json-encoded data in a postgres database?
Body: I need to access the data within JSON data stored in a postgres database. Once I connect, I can see that the data is there, but I don't know how to get the individual elements either in SQL or in R.
dbGetQuery(con, "select id from mytable
where cast(valtext -> 'customer' as text) like '%William%'")
# Error in result_create(conn@ptr, statement) (from functions.R#284) :
# ERROR: operator does not exist: text -> unknown
# LINE 2: where cast(valtext -> 'customer' as text) l...
# ^
# HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
您可以使用类似的方式检索单个组件:
dbGetQuery(con, "select jsontext -> 'items' -> 'qty' as quantity from mytable
where cast(jsontext -> 'items' -> 'product' as text) like '%Toy%'")
# quantity
# 1 1
# 2 2
提取JSON in-R
ret <- dbGetQuery(con, "select jsontext from mytable
where cast(jsontext -> 'items' -> 'product' as text) like '%Toy%'")
ret
# jsontext
# 1 { "customer": "Josh William", "items": {"product": "Toy Car","qty": 1}}
# 2 { "customer": "Mary Clark", "items": {"product": "Toy Train","qty": 2}}
jsonlite::stream_in(textConnection(ret$jsontext))
# Imported 2 records. Simplifying...
# customer items.product items.qty
# 1 Josh William Toy Car 1
# 2 Mary Clark Toy Train 2
我正冒险去推断你在这里真正需要什么。下面是我要回答的问题:
样本数据
一个精心设计的问题应该包括示例数据,所以我将在这里添加一个示例表。此数据改编自http://www.postgresqltutorial.com/postgresql-json/:
在数据库中提取JSON
如果您继续本教程,您将看到Postgres包含两个运算符,它们只适用于
^{pr2}$JSON
类型的字段:但不是
TEXT
或类似的:您可以使用类似的方式检索单个组件:
提取JSON in-R
暴力(但仍然是功能性的)方法是在每个字段上应用
fromJSON
函数。(我使用的是jsonlite
,但我认为RJSONIO
在这里也同样适用:jsonlite
提供的另一种方法是通过jsonlite::stream_in
;我尝试使用RJSONIO::readJSONStream
但无法使其工作。我没有努力,我希望也一样容易。在还有},这是默认操作
jsonlite::fromJSON
的选项可以与jsonlite::stream_in
一起使用:当数据以比上面更复杂的方式嵌套时,我经常需要simplifyDataFrame=FALSE
(注意,包含两个元素的“items”字典是如何“扁平化”成"items.product"
和{simplifyDataFrame=TRUE
的副作用)。在本教程还有更多内容,可能还有无数其他资源。在
相关问题 更多 >
编程相关推荐