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

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more