Public
Edited
Sep 21, 2023
Insert cell
Insert cell
### _D76 is the online database for Detailed Mortality 1999-2013_
Insert cell
jsonObj = xmlToJSON(cdcData)
Insert cell
Insert cell
import {fetchp} from '@tomlarkworthy/fetchp'
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
xmlToJSON = (src) => domToJSON(parser.parseFromString(src, "application/xml"))
Insert cell
cdcData = (await fetchp("https://wonder.cdc.gov/controller/datarequest/D76", { // petch with proxy
method: 'POST',
headers: {
"request_xml":
//xmlParameters by year and race
{"b_parameters" : {
"B_1": "D76.V1-level1",
"B_2": "D76.V8",
"B_3": "*None*",
"B_4": "*None*",
"B_5": "*None*"}
},
"accept_datause_restrictions": "true",
}
})
.then(response => response.text())).replace(/\r/g,'').replace(/\t/g,'')
// .then(response => console.log(response))

Insert cell
parser = new DOMParser()
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