# JSON3: create json using Dict() with integer keys

**URL:** <https://discourse.julialang.org/t/json3-create-json-using-dict-with-integer-keys/29707>\
**Category:** New to Julia\
**Created:** [October 10, 2019, 1:44am UTC](https://discourse.julialang.org/t/json3-create-json-using-dict-with-integer-keys/29707 "2019-10-10T01:44:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elmer\_Cusipuma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmer_cusipuma/32/11958_2.png) [@Elmer\_Cusipuma](https://discourse.julialang.org/u/Elmer_Cusipuma)\
**Post date:** [October 10, 2019, 1:44am UTC](https://discourse.julialang.org/t/json3-create-json-using-dict-with-integer-keys/29707/1 "2019-10-10T01:44:04Z")

</div>

I have found that when the following is run, JSON’s converts int dictionary keys to strings.

```julia
julia> using JSON3
julia> a1 = Dict(1 => "one", 2 => "two", 3 => "three");
julia> json = JSON3.write(a1)
"{\"2\":\"two\",\"3\":\"three\",\"1\":\"one\"}"

```

```julia
julia> a2 = JSON3.read(json,Dict{Int64,String})
ERROR: MethodError: no method matching Int64(::String)

```

Is there any way to preserve the key as an int?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 10, 2019, 6:01am UTC](https://discourse.julialang.org/t/json3-create-json-using-dict-with-integer-keys/29707/2 "2019-10-10T06:01:31Z")

</div>

AFAIK [JSON keys are strings](https://json.org/), no other types are allowed. You could parse to `Int` after reading.

---

<div class="post-metadata">

**Author:** ![Elmer\_Cusipuma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmer_cusipuma/32/11958_2.png) [@Elmer\_Cusipuma](https://discourse.julialang.org/u/Elmer_Cusipuma)\
**Post date:** [October 11, 2019, 2:11am UTC](https://discourse.julialang.org/t/json3-create-json-using-dict-with-integer-keys/29707/3 "2019-10-11T02:11:05Z")

</div>

@Tamas_Papp Thank you so much for your help.

Could you please provide a short example of parsing a Julia Dictionary from string to int? In the case the dictionary contains large amount of keys is this an efficient way to do it?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 11, 2019, 5:53am UTC](https://discourse.julialang.org/t/json3-create-json-using-dict-with-integer-keys/29707/4 "2019-10-11T05:53:03Z")

</div>

Continuing your example, something like

```julia
a2 = JSON3.read(json)
function keys_to_int(dict)
    Dict([parse(Int, string(key)) => val for (key, val) in pairs(dict)])
end
keys_to_int(a2)

```

should work.
