# Serializing composite type to json

**URL:** <https://discourse.julialang.org/t/serializing-composite-type-to-json/1055>\
**Category:** General Usage\
**Tags:** json\
**Created:** [December 20, 2016, 8:56am UTC](https://discourse.julialang.org/t/serializing-composite-type-to-json/1055 "2016-12-20T08:56:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_Langel](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@Frank\_Langel](https://discourse.julialang.org/u/Frank_Langel)\
**Post date:** [December 20, 2016, 8:56am UTC](https://discourse.julialang.org/t/serializing-composite-type-to-json/1055/1 "2016-12-20T08:56:22Z")

</div>

Hi,

i have a

```julia
type foo 
       id::String
   ..
end

```

How to I serialize this to JSON easily ?

Looking for

```julia
JSON.toJson(Foo("Hello"))

```

JSON.jl accepts dictionaries only :=(

Thanks a lot  
Frank

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [December 20, 2016, 9:07am UTC](https://discourse.julialang.org/t/serializing-composite-type-to-json/1055/2 "2016-12-20T09:07:23Z")

</div>

I have been using `JSON` to serialize composite types happily for a while. In my case it was `immutable` instead of `type`, though.

See [this example](https://github.com/leethargo/PipeLayout.jl/blob/master/data/make_instances.jl#L94). The output files are in the same folder.

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [December 20, 2016, 9:09am UTC](https://discourse.julialang.org/t/serializing-composite-type-to-json/1055/3 "2016-12-20T09:09:07Z")

</div>

It works for me:

```julia
julia> using JSON

julia> type Foo
           a::String
           b::Float64
       end

julia> JSON.json(Foo("xyz",1.2))
"{\"a\":\"xyz\",\"b\":1.2}"

```

---

<div class="post-metadata">

**Author:** ![Frank\_Langel](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@Frank\_Langel](https://discourse.julialang.org/u/Frank_Langel)\
**Post date:** [December 22, 2016, 4:57pm UTC](https://discourse.julialang.org/t/serializing-composite-type-to-json/1055/4 "2016-12-22T16:57:18Z")

</div>

My apologies. I misread the documentation, assuming it worked only for dictionaries… THANKS

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 23, 2016, 12:14am UTC](https://discourse.julialang.org/t/serializing-composite-type-to-json/1055/5 "2016-12-23T00:14:55Z")

</div>

There’s no substitute for just trying something to see if it works!
