# Question: Is there a function to flatten a nested dictionary?

**URL:** https://discourse.julialang.org/t/question-is-there-a-function-to-flatten-a-nested-dictionary/54462
**Category:** General Usage
**Tags:** json, dictionary
**Created:** [February 2, 2021, 2:09pm UTC](https://discourse.julialang.org/t/question-is-there-a-function-to-flatten-a-nested-dictionary/54462 "2021-02-02T14:09:11Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@florian](https://discourse.julialang.org/u/florian)
#### Post date: [February 2, 2021, 2:09pm UTC](https://discourse.julialang.org/t/question-is-there-a-function-to-flatten-a-nested-dictionary/54462/1 "2021-02-02T14:09:11Z")

</div>

Hi everyone, I’m facing the following problem: Suppose I have a nested dictionary like

```julia
data = Dict(
    "id" => 42,
    "name" => Dict("first" => "John", "last" => "Doe", "nick" => nothing))

```

which I want to flatten into:

```julia
data_flat = Dict(
    "id" => 42,
    "name.first" => "John",
    "name.last" => "Doe",
    "name.nick" => nothing
)

```

My question is the following: Is there function that does this? Something like flatten\_dict(dict, sep=“.”), or would I have to solve this manually by looping through my nested data? I looked trough the JSON.jl package but either there is no such thing or I missed it.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [February 2, 2021, 2:52pm UTC](https://discourse.julialang.org/t/question-is-there-a-function-to-flatten-a-nested-dictionary/54462/2 "2021-02-02T14:52:53Z")

</div>

I am not sure if there is something that fits your exact need, but it does not seem something so hard to implement.

```julia
function rec_flatten_dict(d, prefix_delim = ".")
    new_d = empty(d)
    for (key, value) in pairs(d)
        if isa(value, Dict)
             flattened_value = rec_flatten_dict(value, prefix_delim)
             for (ikey, ivalue) in pairs(flattened_value)
                 new_d["$key.$ikey"] = ivalue
             end
        else
            new_d[key] = value
        end
    end
    return new_d
end

```

Creates some intermediary structs, so it is not a solution with the greatest performance, but it does the job.

```julia
julia> rec_flatten_dict(data)
Dict{String,Any} with 4 entries:
  "name.last" => "Doe"
  "name.nick" => nothing
  "name.first" => "John"
  "id" => 42

```

---

<div class="post-metadata">

### Author: ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)
#### Post date: [February 2, 2021, 2:56pm UTC](https://discourse.julialang.org/t/question-is-there-a-function-to-flatten-a-nested-dictionary/54462/3 "2021-02-02T14:56:21Z")

</div>

> [@Henrique\_Becker](#):
>
> ```julia
> if isa(value, Dict)
> rec_flatten_dict(value, prefix_delim)
> for (ikey, ivalue) in pairs(value)
> new_d["$key.$ikey"] = ivalue
> end
> else
> 
> ```

Since this doesn’t work in place, you could change the recursion like s:

```julia
if isa(value, Dict)
             flattened_value = rec_flatten_dict(value, prefix_delim)
             for (ikey, ivalue) in pairs(flattened_value)
                 new_d["$key.$ikey"] = ivalue
             end
        else

```

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [February 2, 2021, 2:58pm UTC](https://discourse.julialang.org/t/question-is-there-a-function-to-flatten-a-nested-dictionary/54462/4 "2021-02-02T14:58:22Z")

</div>

Well spotted, I will fix it.

---

<div class="post-metadata">

### Author: ![florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@florian](https://discourse.julialang.org/u/florian)
#### Post date: [February 2, 2021, 3:08pm UTC](https://discourse.julialang.org/t/question-is-there-a-function-to-flatten-a-nested-dictionary/54462/5 "2021-02-02T15:08:43Z")

</div>

Thank you for your answer, and for even providing working code! I thought before I try to do it manually I should ask if I missed something that’s already available.
