-
Hi everybody, I'm just getting started with Danfo.js. I have data to process from a MongoDB that comes in JSON format. The data in the JSON has multiple levels of nesting, something like this, for example (grossly simplified): [
{
"id": 1,
"foo": {
"bar": {
"baz: 23
}
}
},
{
"id": 2,
"foo": {
"bar": {
"baz: 99
}
}
}
] If I understand correctly, it is not possible to import this kind of JSON data into a Danfo data frame directly because of the nesting. For now, I've resorted to writing my own “flattening” function that converts the data into something like this: [
{
"id": 1,
"foo_bar_baz": 23
},
{
"id": 2,
"foo_bar_baz": 99
}
] This data can now be consumed by I'm wondering, though – is this the correct approach? Is there an easier way, which I'm missing? Thanks in advance for your help! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
@pahund Yes, Danfo does not support nested JSON files. |
Beta Was this translation helpful? Give feedback.
-
Perhaps this is useful to somebody – this is the function I'm using to flatten my nested JSON object: function flatten(value, key) {
if (typeof value === "number") {
return { [key]: value };
}
return Object.entries(value).reduce(
(acc, [k, v]) => ({ ...acc, ...flatten(v, `${key ? `${key}_` : ""}${k}`) }),
{}
);
}
const rawData = [
{
id: 1,
foo: {
bar: {
baz: 23,
},
},
},
{
id: 2,
foo: {
bar: {
baz: 99,
},
},
},
];
const flatData = rawData.map((item) => flatten(item));
// result: [ { id: 1, foo_bar_baz: 23 }, { id: 2, foo_bar_baz: 99 } ] |
Beta Was this translation helpful? Give feedback.
@pahund Yes, Danfo does not support nested JSON files.