# How to create a dictionary of dictionaries from an n-dimensional array?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-dictionary-of-dictionaries-from-an-n-dimensional-array/66605>\
**Category:** New to Julia\
**Tags:** json, array, dictionary\
**Created:** [August 18, 2021, 2:53pm UTC](https://discourse.julialang.org/t/how-to-create-a-dictionary-of-dictionaries-from-an-n-dimensional-array/66605 "2021-08-18T14:53:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch/32/24224_2.png) [@Mitch](https://discourse.julialang.org/u/Mitch)\
**Post date:** [August 18, 2021, 2:53pm UTC](https://discourse.julialang.org/t/how-to-create-a-dictionary-of-dictionaries-from-an-n-dimensional-array/66605/1 "2021-08-18T14:53:28Z")

</div>

Thanks for your help!

I need to create a nested JSON file and the first step is to create a dictionary of dictionaries. The below should provide a minimum working example.

```julia
Pkg.add("Random")

#Sample of desired keys and categories randomly generated
investmentkeys = string.(zeros(110))
yearkeys = string.(collect(1:12))
valuekeys = string.(zeros(13))

for i in 1:length(investmentkeys)
    investmentkeys[i] = randstring(5)
end

for i in 1:length(valuekeys)
    valuekeys[i] = randstring(3)
end

#Generating sample data
data = rand(length(investmentkeys), length(yearkeys), 
    length(valuekeys))

#Create the first level dictionary
dictdata = Dict(agekeys[i] => data[i,:,:] for i=1:length(investmentkeys)
Dict{String, Matrix{Float64}}

```

The highest level of the dictionary should be the 110 investment names; each contain a 12x13 matrix.  
I’d like to then use the yearkeys to split each matrix into a subset dictionary, which contrains a vector of values. I think the result would be:

```julia
Dict{String, Dict(String, Vector{Float64}}

```

How can I create a dictionary of dictionaries with a for loop?

Thank you!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 18, 2021, 5:38pm UTC](https://discourse.julialang.org/t/how-to-create-a-dictionary-of-dictionaries-from-an-n-dimensional-array/66605/2 "2021-08-18T17:38:03Z")

</div>

> [@Mitch](#):
>
> It looks like JSON is the best export format as each subsequent dimension is a subset of the prior dimension, but let me know if that’s wrong!

JSON is a pretty terrible format for multidimensional arrays, in my opinion — something like HDF5 (or VTK or NetCDF) would be much better because it can directly represent multidimensional arrays (rather than nested lists of lists etc.). However, I don’t know anything about PowerBI or what formats it supports.

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [August 18, 2021, 5:54pm UTC](https://discourse.julialang.org/t/how-to-create-a-dictionary-of-dictionaries-from-an-n-dimensional-array/66605/3 "2021-08-18T17:54:38Z")

</div>

If you can post a small example of what your input data looks like and then an example of what the desired output is, it’s more likely that someone will be able to help. I use Julia to write JSON objects fairly regularly but it’s easiest to just see what your data looks like and what the JSON object should look like rather than describing it.

---

<div class="post-metadata">

**Author:** ![Mitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch/32/24224_2.png) [@Mitch](https://discourse.julialang.org/u/Mitch)\
**Post date:** [August 18, 2021, 6:58pm UTC](https://discourse.julialang.org/t/how-to-create-a-dictionary-of-dictionaries-from-an-n-dimensional-array/66605/4 "2021-08-18T18:58:02Z")

</div>

Hey @mthelm85, I think I’ve edited the post to include something resembling a minimum working example; thanks for your help and let me know if there’s anything else I can clarify!

---

<div class="post-metadata">

**Author:** ![Mitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch/32/24224_2.png) [@Mitch](https://discourse.julialang.org/u/Mitch)\
**Post date:** [August 18, 2021, 8:50pm UTC](https://discourse.julialang.org/t/how-to-create-a-dictionary-of-dictionaries-from-an-n-dimensional-array/66605/5 "2021-08-18T20:50:09Z")

</div>

Thanks for the suggestion! I’ve Googled around and it looks like PowerBI doesn’t support those formats, unfortunately. I might be wrong though!
