# Transferring data structures between Julia files?

**URL:** https://discourse.julialang.org/t/transferring-data-structures-between-julia-files/49636
**Category:** General Usage
**Created:** [November 5, 2020, 6:57pm UTC](https://discourse.julialang.org/t/transferring-data-structures-between-julia-files/49636 "2020-11-05T18:57:21Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [November 5, 2020, 6:57pm UTC](https://discourse.julialang.org/t/transferring-data-structures-between-julia-files/49636/1 "2020-11-05T18:57:21Z")

</div>

I need to transfer information between two Julia files (or: between two Jupyter notebooks). Examples of data structures:

- A dictionary with keys “a”, “b”, “c”, etc., where the value for each key is a tuple holding Floats, dates, interpolation functions, etc.
- It is kind of ugly to write this into a \*.jl file to include the file in the other document.

**Question** : is there a simpler way? Can I store the data in a Json file and read the JSON file in the other document? If so:

- Which package?

Example: suppose I have the following data structure:

```julia
using Dates, DataInterpolations
#
x = 3
t = Date(2020,11,5)
X = 0:20
Y = sin.(X*2pi/10)
y = ConstantInterpolation(Y,X)
#
d = Dict("a" => (x,t,y))
# ...
merge(d,Dict("b") => ...)

```

If I want to store this as a \*.jl file, I cannot save/write the dictionary `d`, but instead need to build it up from scratch in write statements… something like:

```julia
open("my_dictionary.jl", "w") do f
    write(f,"t_y = $(Year(t).value) \n")
    write(f,"t_m = $(Month(t).value) \n")
    write(f,"t_d = $(Day(t).value) \n")
    write(f,"X = $(X) \n")
    write(f,"Y = $(Y) \n")
    #
    write(f,"y = ConstantInterpolation(Y,X) \n")
    write(f,"d = Dict("a",($(x),Date(t_y,t_m,t_d),y)) \n")
end

```

When I run this code, I can include the file in another Jupyter notebook, and get back the dictionary:

```julia
using Dates, DataInterpolations, Plots
include("my_dictionary.jl")
plot(d["a"][3])

```

I _assume_ there is a simpler way, so that I only need to write something like

```julia
save("my_dictionary",$d)

```

or something like this…

But what is the best package to do this?

---

<div class="post-metadata">

### Author: ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)
#### Post date: [November 5, 2020, 7:10pm UTC](https://discourse.julialang.org/t/transferring-data-structures-between-julia-files/49636/2 "2020-11-05T19:10:48Z")

</div>

Maybe try the JLD2.jl or BSON.jl packages?

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [November 5, 2020, 7:14pm UTC](https://discourse.julialang.org/t/transferring-data-structures-between-julia-files/49636/3 "2020-11-05T19:14:42Z")

</div>

OK – I’ve seen a `JSON` package, and a `JSON2` package. And then there is the `JLD` and the `JLD2` package. And a `BSON` package.

But which do you recommend? Are all maintained? Simple to use vs. comprehensive?

---

<div class="post-metadata">

### Author: ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)
#### Post date: [November 5, 2020, 7:15pm UTC](https://discourse.julialang.org/t/transferring-data-structures-between-julia-files/49636/4 "2020-11-05T19:15:18Z")

</div>

I have used and like all of the following packages:

1. JLD2
2. JSON3
3. BSON

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [November 5, 2020, 7:16pm UTC](https://discourse.julialang.org/t/transferring-data-structures-between-julia-files/49636/5 "2020-11-05T19:16:27Z")

</div>

OK – in that order? Or all are good?

---

<div class="post-metadata">

### Author: ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)
#### Post date: [November 5, 2020, 7:17pm UTC](https://discourse.julialang.org/t/transferring-data-structures-between-julia-files/49636/6 "2020-11-05T19:17:11Z")

</div>

All three of the packages that I listed are good. I would recommend starting with JLD2.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [November 5, 2020, 7:17pm UTC](https://discourse.julialang.org/t/transferring-data-structures-between-julia-files/49636/7 "2020-11-05T19:17:48Z")

</div>

Thanks! You have limited my need to read lots of documentation :-).

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [November 5, 2020, 11:06pm UTC](https://discourse.julialang.org/t/transferring-data-structures-between-julia-files/49636/8 "2020-11-05T23:06:25Z")

</div>

You could also look at the standard library `Serialization`.  
[https://docs.julialang.org/en/v1/stdlib/Serialization/](https://docs.julialang.org/en/v1/stdlib/Serialization/)

Might be suitable if you are always transferring across the same Julia versions.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [November 6, 2020, 8:08am UTC](https://discourse.julialang.org/t/transferring-data-structures-between-julia-files/49636/9 "2020-11-06T08:08:52Z")

</div>

I use different versions of Julia on various computers. So: v. 1.4.2 on my home computer, 1.5.1 on my other computers.

From your comment, it sounds like this might be a problem with `Serialization`? Or do you mean the problem is in Windows version vs. Mac version, etc.?
