# Probe if a variable exists in a JLD2-file

**URL:** https://discourse.julialang.org/t/probe-if-a-variable-exists-in-a-jld2-file/88969
**Category:** General Usage
**Tags:** jld2
**Created:** [October 19, 2022, 2:58pm UTC](https://discourse.julialang.org/t/probe-if-a-variable-exists-in-a-jld2-file/88969 "2022-10-19T14:58:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [October 19, 2022, 2:58pm UTC](https://discourse.julialang.org/t/probe-if-a-variable-exists-in-a-jld2-file/88969/1 "2022-10-19T14:58:43Z")

</div>

Is there a way to probe / evaluate if a variable exists in a JLD2-file?

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [October 19, 2022, 3:28pm UTC](https://discourse.julialang.org/t/probe-if-a-variable-exists-in-a-jld2-file/88969/2 "2022-10-19T15:28:02Z")

</div>

ok one way is to load the whole content in a single dict and use `haskey`:

```julia
haskey(load("filename.jld2"), "varable_name_to_probe")

```

But for larger jld2-files that might not be the best option.  
Are there other ideas?

---

<div class="post-metadata">

### Author: ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)
#### Post date: [October 19, 2022, 3:28pm UTC](https://discourse.julialang.org/t/probe-if-a-variable-exists-in-a-jld2-file/88969/3 "2022-10-19T15:28:21Z")

</div>

Hi @ellocco,

this should do what you want, no?  
There’s the github page and the linked docs with more info: [GitHub - JuliaIO/JLD2.jl: HDF5-compatible file format in pure Julia](https://github.com/JuliaIO/JLD2.jl)

```julia
julia> using JLD2
[Info: Precompiling JLD2 [033835bb-8acc-5ee8-8aae-3f567f8a3819]

julia> save("test.jld2"^C

julia> f = jldopen("test.jld2", "w")
JLDFile /home/isensee/test.jld2 (read/write)
  (no datasets)

julia> f["a"] = 42
42

julia> close(f)

julia> f = jldopen("test.jld2")
JLDFile /home/isensee/test.jld2 (read-only)
 └─🔢 a

julia> keys(f)
1-element Vector{String}:
 "a"

julia> haskey(f, "a")
true

julia> f["a"]
42

julia> close(f)

```

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [October 19, 2022, 3:31pm UTC](https://discourse.julialang.org/t/probe-if-a-variable-exists-in-a-jld2-file/88969/4 "2022-10-19T15:31:59Z")

</div>

@JonasIsensee Hi,  
I read the documentation and searched in the net.  
But I was not creative enough to use `haskey`. Thanks for your support!
