# Convert type to dictionary

**URL:** <https://discourse.julialang.org/t/convert-type-to-dictionary/6982>\
**Category:** General Usage\
**Created:** [November 9, 2017, 5:37pm UTC](https://discourse.julialang.org/t/convert-type-to-dictionary/6982 "2017-11-09T17:37:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [November 9, 2017, 5:37pm UTC](https://discourse.julialang.org/t/convert-type-to-dictionary/6982/1 "2017-11-09T17:37:45Z")

</div>

Is there a way to convert a `type` into a `Dict`?  
Or is there a way to get a list of keys inside a type?

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [November 9, 2017, 5:45pm UTC](https://discourse.julialang.org/t/convert-type-to-dictionary/6982/2 "2017-11-09T17:45:59Z")

</div>

`fieldnames(type)` will return a `Vector{Symbol}`

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [November 9, 2017, 6:07pm UTC](https://discourse.julialang.org/t/convert-type-to-dictionary/6982/3 "2017-11-09T18:07:12Z")

</div>

I think what you are looking for would be

```julia
typedict(x) = Dict(fn=>getfield(x, fn) for fn ∈ fieldnames(x)) 

```

(in case you were not aware of `getfield`).

---

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [November 9, 2017, 6:14pm UTC](https://discourse.julialang.org/t/convert-type-to-dictionary/6982/4 "2017-11-09T18:14:20Z")

</div>

Perfect thanks! Yes getfield doesn’t appear here: [https://docs.julialang.org/en/stable/manual/types/](https://docs.julialang.org/en/stable/manual/types/)

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [November 9, 2017, 6:21pm UTC](https://discourse.julialang.org/t/convert-type-to-dictionary/6982/5 "2017-11-09T18:21:43Z")

</div>

For stuff like that you want to look in the standard library documentation [here](https://docs.julialang.org/en/stable/stdlib/base/).

By the way, for what it’s worth, I recommend you avoid doing this if possible. In the general case you will have a `Dict{Symbol,Any}` so the compiler will no longer know the types of the fields. If you simply leave your `struct` intact, you won’t have this problem.

---

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [November 9, 2017, 6:29pm UTC](https://discourse.julialang.org/t/convert-type-to-dictionary/6982/6 "2017-11-09T18:29:33Z")

</div>

Thanks for the link. I’ll just use it for pretty printing

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [December 15, 2020, 8:31am UTC](https://discourse.julialang.org/t/convert-type-to-dictionary/6982/7 "2020-12-15T08:31:22Z")

</div>

Came across this, and just wanted to correct it slightly to:

```julia
typedict(x::T) where {T} = Dict(fn=>getfield(x, fn) for fn ∈ fieldnames(T))

```

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [May 24, 2021, 2:11am UTC](https://discourse.julialang.org/t/convert-type-to-dictionary/6982/8 "2021-05-24T02:11:56Z")

</div>

> [@ExpandingMan](#):
>
> `typedict(x) = Dict(fn=>getfield(x, fn) for fn ∈ fieldnames(x))`

👍

For a `struct` the syntax now requires a wrap with `typeof(x)`:

`typedict(x) = Dict(fn=>getfield(x, fn) for fn ∈ fieldnames(typeof(x)))`

To preserve the order of the fields, you could use `OrderedDict` instead of `Dict` or, for small `structs`, `LittleDict` is faster and preserves order (both are from `OrderedCollections`) .
