A way to handle nested objects #661
Answered
by
nshiab
graemebruce
asked this question in
Q&A
-
When bringing in json data using loadData or loadGeoData, something is needed to handle nested objects. Example dataset: Column "metobject" |
Beta Was this translation helpful? Give feedback.
Answered by
nshiab
Aug 8, 2024
Replies: 1 comment
-
You can access the values in nested objects by casting them to JSON. Create a new folder and run The casting to JSON happens on line 24. import { SimpleDB } from "simple-data-analysis";
import { prettyDuration } from "journalism";
const start = Date.now();
// We set a maximum width to the string columns to keep it readable.
const sdb = new SimpleDB({ nbCharactersToLog: 50 });
const table = sdb.newTable();
// We cache so we don't fetch everytime while working on the file.
await table.cache(async () => {
await table.loadGeoData(
"https://dd.weather.gc.ca/hurricanes/20240802T1500Z_MSC_Hurricane_FOUR.json"
);
// Some geometries are null. We exclude them.
await table.filter(`geom NOT NULL`);
});
// We extract the classification after casting to JSON.
await table.addColumn(
`classification`,
"string",
`CAST(metobject AS JSON).classification`
);
// We clean up the extra quotes around classifications.
await table.replace("classification", { '"': "" });
await table.logTable();
prettyDuration(start, { log: true, prefix: "\nDone in " }); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
graemebruce
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can access the values in nested objects by casting them to JSON.
Create a new folder and run
npx simple-data-analysis
, copy-paste the code below into yourindex.js
orindex.ts
, then runnpm run sda
and you should see a new columnclassification
at the end of the table.The casting to JSON happens on line 24.