Created
March 15, 2013 23:08
-
-
Save adambom/5173889 to your computer and use it in GitHub Desktop.
Pseudocode for generating multidimensional json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Data structure to use for household size, e.g. | |
{ | |
"northeast": { | |
"hispanic": { | |
"below 35": { | |
"under $20,000": [] | |
} | |
} | |
}, | |
"southeast": { | |
"hispanic": { | |
"below 35": { | |
"under $20,000": [] | |
} | |
} | |
} | |
... | |
} | |
# How to generate the above data structure | |
data = Dict() | |
for region in regions, race in races, age in ages, income in incomes | |
if !has(data, region) | |
data[region] = Dict() | |
end | |
if !has(data[region], race) | |
data[region][race] = Dict() | |
end | |
... | |
if !has(data[region][race][age][income], race) | |
data[region][race][age][income] = [] | |
end | |
end | |
# Parse csv file and turn it into json | |
rows = read_csv("census.csv") | |
for each row in rows | |
region = row[1] | |
age = row[2] | |
income = row[3] | |
race = row[4] | |
household = row[5] | |
push!(data[region][race][age][income], household) | |
end | |
for region in regions, race in races, age in ages, income in incomes | |
# aggregate might be a summation, average, hist | |
aggregate(data[region][race][age][income]) | |
end | |
# Write it out | |
write_to_file(data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment