r - R:如何将嵌套的 JSON 转换为数据框
问题描述
我有嵌套的 JSON(行)文件,看起来像
[{"ok":2, "nested": {"meh":2, "hehe":{"a":1, "b":2 }}},
{"ok":3, "nested": {"meh":10, "hehe":{"a":1, "b":2}}},
{"ok":4, "nested": {"meh":11, "hehe":{"a":1, "b":2}}}]
...
我希望将其取消嵌套到 data.frame 表示的 CSV 表单中
ok, nested.meh, nested.hehe.a, nested.hehe.b
2, 2, 1, 2
3, 10, 1, 2
4, 11, 1, 2
...
我试过了
a = jsonlite::read_json("file.json")
但是然后是一个列表列表,它似乎需要自定义编码才能取消嵌套,但我想阅读一般的嵌套文件。有包来处理这个吗?我查看了文档,rlist
但找不到信息,因为rlist::table
它不起作用。
解决方案
我通常会选择 flatten_json 包
import flatten_json
ex_json = [{"ok":2, "nested": {"meh":2, "hehe":{"a":1, "b":2 }}},
{"ok":3, "nested": {"meh":10, "hehe":{"a":1, "b":2}}},
{"ok":4, "nested": {"meh":11, "hehe":{"a":1, "b":2}}}]
ex_df = pd.io.json.json_normalize(flatten_json.flatten_json(ex_json[0], separator="."))
推荐阅读
- javascript - 选中一个复选框并单击按钮并获取值时如何选中所有复选框?
- jooq - DSLContext.ddl() 似乎不支持类型
- node.js - 如何使用 Node JS 执行带参数的 Powershell 脚本函数?
- python - 如何在 Django 中为具有以下关系约束的注册系统建模?
- node.js - 如何在nodejs中打印[object Object]中的完整数据
- c# - 有没有一种方法可以在一种情况下进一步简化具有多个值的 C# switch 表达式?
- java - 将多个摄像机图像显示到网格视图
- javascript - jwplayer 无法搜索 google photo 的视频
- python - 如何使用数组模块在 Python 中定义二维数组?
- tailwind-css - 在 div 或组件上禁用 Tailwind