# JSON3 to mutable Dict{String,Any}

**URL:** <https://discourse.julialang.org/t/json3-to-mutable-dict-string-any/93443>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [January 24, 2023, 7:07am UTC](https://discourse.julialang.org/t/json3-to-mutable-dict-string-any/93443 "2023-01-24T07:07:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JesperMartinsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jespermartinsson/32/34098_2.png) [@JesperMartinsson](https://discourse.julialang.org/u/JesperMartinsson)\
**Post date:** [January 24, 2023, 7:07am UTC](https://discourse.julialang.org/t/json3-to-mutable-dict-string-any/93443/1 "2023-01-24T07:07:02Z")

</div>

I’m using `JSON3` to parse an input that can be either a json or an array of json string. I want to convert the output of `JSON3.read` to either a mutable `Dict{String,Any}` or `Array{Dict{String,Any}}`, respectively. I see that `copy(JSON3.read(...))` always returns keys as `Symbol` and not `String`. The only solution I have so far to return keys as `String` is to using a sink argument that is a union: `JSON3.read(input,Union{Dict{String,Any},Array{Dict{String,Any}}})`. Anyone know of a preferred way to do this or is this the way to go?

I’ll provide a MWE below to play with:

```julia
using JSON3

# create input to play with
d1 = Dict("a"=>1, :b=>Dict("c"=>2, :d=>3))
d2 = [Dict("a"=>1, :b=>Dict("c"=>2, :d=>3)) for _ in 1:2]

# create json strings of the input 
d1_json = JSON3.write(d1)
d2_json = JSON3.write(d2)

# returns a Dic{Symbol,Any} and Vector{Dict{Symbol,Any}} but not String as keys   
d12 = copy(JSON3.read(d1_json))
d22 = copy(JSON3.read(d2_json))

# returns String as keys but is this the right way to go?
d13 = JSON3.read(d1_json,Union{Dict{String,Any},Array{Dict{String,Any}}})
d23 = JSON3.read(d2_json,Union{Dict{String,Any},Array{Dict{String,Any}}})

```

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [February 3, 2023, 1:54am UTC](https://discourse.julialang.org/t/json3-to-mutable-dict-string-any/93443/2 "2023-02-03T01:54:39Z")

</div>

That looks to be about the best you can do at the moment. I’m [working on something](https://github.com/quinnj/JSONBase.jl) that would make this much more straightforward, but it’s not quite ready for public consumption yet.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 3, 2023, 2:43am UTC](https://discourse.julialang.org/t/json3-to-mutable-dict-string-any/93443/3 "2023-02-03T02:43:00Z")

</div>

Use JSON?

---

<div class="post-metadata">

**Author:** ![JesperMartinsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jespermartinsson/32/34098_2.png) [@JesperMartinsson](https://discourse.julialang.org/u/JesperMartinsson)\
**Post date:** [February 3, 2023, 1:57pm UTC](https://discourse.julialang.org/t/json3-to-mutable-dict-string-any/93443/4 "2023-02-03T13:57:26Z")

</div>

Thanks for the advice. `JSON` was my first attempt but it did not handle nested jsons well so I tried `JSON3` and that did it without any issues or workarounds. I also liked that I can access `JSON3` object’s fields in may ways, however, when I convert it to a mutable object that functionality was lost and I needed `keys` as `String`.
