错误:无效的JSON对象

时间:2017-02-20 18:17:55

标签: json r mongodb

我尝试运行此查询并继续出现此错误:

install.packages(“mongolite”)
library(mongolite)

m <- mongo(db = "ionmom")
m6 <- m$aggregate('[{"$unwind":"$cdr"}, {$lookup:{from: "inventory", localField: "_id", foreignField: "_id", as:"inventory"}},{$unwind: "$inventory"}, {"$project":{ "$project": {"cdr.duration": 1, "inventory.wearables.type":1, "inventory.wearables.status":1, "inventory.wearables.battery":1 }}}]')

# Error: Invalid JSON object: [{"$unwind":"$cdr"}, {$lookup:{from: "inventory", localField: "_id", foreignField: "_id", as:"inventory"}},{$unwind: "$inventory"}, {"$project":{ "$project": {"cdr.duration": 1, "inventory.wearables.type":1, "inventory.wearables.status":1, "inventory.wearables.battery":1 }}}]

2 个答案:

答案 0 :(得分:3)

mongolite使用jsonlite来进行JSON解析。如果您通过jsonlite::fromJSON()查询,则会看到问题

js <- '[{"$unwind":"$cdr"}, {$lookup:{from: "inventory", localField: "_id", foreignField: "_id", as:"inventory"}},{$unwind: "$inventory"}, {"$project":{ "$project": {"cdr.duration": 1, "inventory.wearables.type":1, "inventory.wearables.status":1, "inventory.wearables.battery":1 }}}]'

jsonlite::fromJSON(js)

# Error: lexical error: invalid char in json text.
#                  [{"$unwind":"$cdr"}, {$lookup:{from: "inventory", loc
#                      (right here) ------^

这告诉你JSON结构无效,因为它期望在每个字符串周围引用" "

js <- '[{"$unwind":"$cdr"}, {"$lookup":{"from": "inventory", "localField": "_id", "foreignField": "_id", "as":"inventory"}},{"$unwind": "$inventory"}, {"$project":{ "$project": {"cdr.duration": 1, "inventory.wearables.type":1, "inventory.wearables.status":1, "inventory.wearables.battery":1 }}}]'

m$aggregate(js)

## I don't have your data ...

# Imported 0 records. Simplifying into dataframe...
# data frame with 0 columns and 0 rows

答案 1 :(得分:0)

尝试添加:

js <- '[{"$unwind":"$cdr"}, {"$lookup":{"from": "inventory", localField:}]'