# How to read tuples with JSON3?

**URL:** https://discourse.julialang.org/t/how-to-read-tuples-with-json3/29843
**Category:** New to Julia
**Created:** [October 13, 2019, 12:35am UTC](https://discourse.julialang.org/t/how-to-read-tuples-with-json3/29843 "2019-10-13T00:35:18Z")
**Posts on this page:** 9
**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 13, 2019, 12:35am UTC](https://discourse.julialang.org/t/how-to-read-tuples-with-json3/29843/1 "2019-10-13T00:35:18Z")

</div>

I am new to JSON3.jl and trying to do the following

```julia
julia> using JSON3

julia> tup = (:car,"Mercedes","S500",5,250.1)
(:car, "Mercedes", "S500", 5, 250.1)

julia> json = JSON3.write(tup)
"[\"car\",\"Mercedes\",\"S500\",5,250.1]"

```

I know I can do this:

```julia
d = JSON3.read(json,Tuple{Symbol,String,String,Int64,Float64})
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Symbol

```

But I get an error reading the object of type symbol

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [October 13, 2019, 1:50am UTC](https://discourse.julialang.org/t/how-to-read-tuples-with-json3/29843/2 "2019-10-13T01:50:54Z")

</div>

I think the reason is that to get `Symbol` from `String` one needs `Meta.parse` instead of `convert`. So it could be a bug somewhere.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [October 13, 2019, 6:11am UTC](https://discourse.julialang.org/t/how-to-read-tuples-with-json3/29843/3 "2019-10-13T06:11:18Z")

</div>

For the time being you could add the conversion:

```julia
using JSON3
Base.convert(::Type{Symbol}, s::String) = Meta.parse(s)

```

then

```julia
julia> JSON3.read(tupjson, tuptype)
(:car, "Mercedes", "S500", 5, 250.1)

julia> tup = (:car,"Mercedes","S500",5,250.1)
(:car, "Mercedes", "S500", 5, 250.1)

julia> tuptype = typeof(tup)
Tuple{Symbol,String,String,Int64,Float64}

julia> tupjson = JSON3.write(tup)
"[\"car\",\"Mercedes\",\"S500\",5,250.1]"

julia> JSON3.read(tupjson, tuptype)
(:car, "Mercedes", "S500", 5, 250.1)

```

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [October 13, 2019, 6:17am UTC](https://discourse.julialang.org/t/how-to-read-tuples-with-json3/29843/4 "2019-10-13T06:17:22Z")

</div>

I raised this issue [https://github.com/quinnj/JSON3.jl/issues/28](https://github.com/quinnj/JSON3.jl/issues/28)

---

<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: [October 13, 2019, 7:53am UTC](https://discourse.julialang.org/t/how-to-read-tuples-with-json3/29843/5 "2019-10-13T07:53:02Z")

</div>

> [@Elmer\_Cusipuma](#):
>
> d = JSON3.read(json,Tuple{Symbol,String,String,Int64,Float64})

This is mainly because JSON3 doesn’t have specialized support for Tuples yet. It supports them via a general “array” interface, but the general array interface assumes the container will have a single `eltype` when reading. In this case, your tuple type is not homogenous, so each element is just parsed generally, then it tries to convert at the end, as opposed to using the types of each element of your tuple and parsing the types specifically. I don’t think it’d be too hard to add support for this though, since tuples can be a really useful way to bundle a couple things together.

---

<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: [October 16, 2019, 3:19am UTC](https://discourse.julialang.org/t/how-to-read-tuples-with-json3/29843/6 "2019-10-16T03:19:42Z")

</div>

Just FYI, just merged support for reading heterogenously-typed tuples in JSON3.jl: [https://github.com/quinnj/JSON3.jl/pull/29](https://github.com/quinnj/JSON3.jl/pull/29).

---

<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 19, 2019, 1:43pm UTC](https://discourse.julialang.org/t/how-to-read-tuples-with-json3/29843/8 "2019-10-19T13:43:27Z")

</div>

@quinnj Now it’s works, thank you so much for your help.

```julia
julia> JSON3.read("[\"car\",\"Mercedes\"]", Tuple{Symbol, String})
(:car, "Mercedes")

```

But another problem arose. I am trying to read a tuple in a struct

```julia
struct data
    t :: Tuple{Symbol, String}
end

```

Getting this error when I run test

```julia
julia> JSON3.StructType(::Type{data}) = JSON3.Struct()

julia> JSON3.read("{\"t\":[\"car\",\"Mercedes\"]}", data)
ERROR: ArgumentError: invalid JSON at byte position 23 while parsing type Tuple{Symbol,String}: ExpectedComma
{"t":["car","Mercedes"]}

```

---

<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: [October 27, 2019, 6:12am UTC](https://discourse.julialang.org/t/how-to-read-tuples-with-json3/29843/9 "2019-10-27T06:12:44Z")

</div>

Hey @Elmer_Cusipuma, sorry for the slow response, I’m trying to catch up on recent pings/issues. There was a bug in the recent tuple support I added to JSON3.jl, so I just pushed a fix and made a new release, so once that’s merged in the registry, this should work as expected.

Sorry for the hassle.

---

<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 28, 2019, 10:44am UTC](https://discourse.julialang.org/t/how-to-read-tuples-with-json3/29843/10 "2019-10-28T10:44:48Z")

</div>

Thanks!
