Public
Edited
Jan 6, 2023
Importers
Insert cell
Insert cell
viewof zipfile = Inputs.file({ accept: ".zip", required: true })
Insert cell
function zipfile_to_json(z) {
const archive = z.zip();
const rawxml = archive.file("apple_health_export/export.xml").text();
const parsed = parser.parseFromString(rawxml, "application/xml");
return domToJSON(parsed);
}
Insert cell
archive = zipfile.zip()
Insert cell
rawxml = archive.file("apple_health_export/export.xml").text()
Insert cell
parsed = parser.parseFromString(rawxml, "application/xml")
Insert cell
healthjson = domToJSON(parsed)
Insert cell
zipfile_to_json(zipfile)
Insert cell
parser = new DOMParser()
Insert cell
function domToJSON(node) {
let obj = Object.create(null);
if (node.children.length === 0 && node.innerHTML) {
obj.content = node.innerHTML;
}

const counts = childCounts(node);

for (const child of node.children) {
const { localName } = child;
if (counts[localName] > 1) {
(obj[localName] = obj[localName] || []).push(domToJSON(child));
} else {
obj[localName] = domToJSON(child);
}
}

let attrs = node.attributes;
if (attrs) {
for (let i = attrs.length - 1; i >= 0; i--) {
obj[attrs[i].name] = attrs[i].value;
}
}

return obj;
}
Insert cell
childCounts = (node) => {
const counts = {};
for (const { localName } of node.children)
counts[localName] = (counts[localName] || 0) + 1;
return counts;
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more