Published
Edited
Jun 22, 2021
Insert cell
Insert cell
Insert cell
jsonstatGet(data, "E06000010", "Indicator:Housing affordability", "2016")
Insert cell
Insert cell
getHousingAffordability = jsonstatGetPartial(
data,
null,
"Indicator:Housing affordability",
null
)
Insert cell
getHousingAffordability("E92000001", "2016")
Insert cell
Insert cell
jsonstatToArray(data)
Insert cell
Insert cell
function jsonstatGet(data, ...keys) {
let index = 0;
keys.forEach((key, i) => {
if (i > 0) index *= data.size[i];
index += data.dimension[data.id[i]].category.index[key];
});
return data.value[index];
}
Insert cell
function jsonstatGetPartial(data, ...partialKeys) {
return (...keys) => {
let i = 0;
return jsonstatGet(
data,
...partialKeys.map(key => (key === null ? keys[i++] : key))
);
};
}
Insert cell
function jsonstatToArray(data) {
let value = {};
let result = [];
let dimensions = data.id.map((id, i) => {
let dims = new Array(data.size[i]);
for (let category in data.dimension[id].category.index) {
dims[data.dimension[id].category.index[category]] = category;
}
return dims;
});
let helper = function(dimensionIndex) {
let dimensionId = data.id[dimensionIndex];
for (let category of dimensions[dimensionIndex]) {
value[dimensionId] = category;
if ("label" in data.dimension[dimensionId].category) {
value[dimensionId + "_label"] =
data.dimension[dimensionId].category.label[category];
}
if (dimensionIndex === dimensions.length - 1) {
value["value"] = data.value[result.length];
result.push({ ...value });
} else {
helper(dimensionIndex + 1);
}
}
};
helper(0);
return result;
}
Insert cell
jsonstatToArray(data)
Insert cell
data = FileAttachment("hibeta.json").json()
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