如何删除从R API返回的json数组的嵌套级别

时间:2016-07-20 08:03:31

标签: json r opencpu

我使用OpenCPU创建了一个R API,用于查询neo4j数据库并返回结果。 结果看起来像

    [ [
        {
      "abc": "aabbcc1",
      "pqr": "ppqqrr1",
      "xyz": "xxyyzz1"
    }
  ],
  [
    {
      "abc": "aabbcc2",
      "pqr": "ppqqrr2",
      "xyz": "xxyyzz2"
    }
  ],
  [
    {
      "abc": "aabbcc3",
      "pqr": "ppqqrr3",
      "xyz": "xxyyzz3"
    }
  ]
]

这里有一个不必要的嵌套级别。我怎么摆脱这个? 我希望json看起来像

 [
    {
      "abc": "aabbcc1",
      "pqr": "ppqqrr1",
      "xyz": "xxyyzz1"
    },
    {
      "abc": "aabbcc2",
      "pqr": "ppqqrr2",
      "xyz": "xxyyzz2"
    },
    {
      "abc": "aabbcc3",
      "pqr": "ppqqrr3",
      "xyz": "xxyyzz3"
    }
  ]

我尝试在R控制台中打印结果,即在转换为JSON之前,它看起来像

[[1]]
                                abc pqr xyz
1 aabbcc1   ppqqrr1 xxyyzz1

[[2]]
                                abc pqr    xyz
1 aabbcc2  ppqqrr2 xxyyzz2

[[3]]
                                abc pqr xyz
2 aabbcc3  ppqqrr3 xxyyzz3

我尝试使用此链接R How to remove a level of lists from a list of lists

中提到的解决方案

之后生成的json看起来像

{"abc":["aabbcc1"],"pqr":["ppqqrr1"],"xyz":["xxyyzz1"],"abc.1":["aabbcc2"],"pqr.1":["ppqqrr2"],"xyz.1":["xxyyzz2"],"abc.2":["aabbcc3"],"pqr.2":["ppqqrr3"],"xyz.2":["xxyyzz3"]}

1 个答案:

答案 0 :(得分:0)

尝试将数据放入jsonlite::rbind.pages(yourdata),然后再将其投放到toJSON