# Parsing a listing of non-rectangular array

**URL:** <https://discourse.julialang.org/t/parsing-a-listing-of-non-rectangular-array/15762>\
**Category:** New to Julia\
**Created:** [October 1, 2018, 10:49pm UTC](https://discourse.julialang.org/t/parsing-a-listing-of-non-rectangular-array/15762 "2018-10-01T22:49:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [October 1, 2018, 10:49pm UTC](https://discourse.julialang.org/t/parsing-a-listing-of-non-rectangular-array/15762/1 "2018-10-01T22:49:42Z")

</div>

I would like to parse strings like this `"[[[1], [2]], [[2, 3], [4, 5]], [[6, 7, 8], [3, 1, 4]]]"` and get the underlying array back. Is there an off-the-shelf function to do it?

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [October 1, 2018, 11:13pm UTC](https://discourse.julialang.org/t/parsing-a-listing-of-non-rectangular-array/15762/2 "2018-10-01T23:13:07Z")

</div>

I found it:

```julia
eval(Meta.parse("[[[1], [2]], [[2, 3], [4, 5]], [[6, 7, 8], [3, 1, 4]]]"))

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 1, 2018, 11:30pm UTC](https://discourse.julialang.org/t/parsing-a-listing-of-non-rectangular-array/15762/3 "2018-10-01T23:30:26Z")

</div>

That does work, but use of `eval` can be slow, unsafe, and very often a sign that there is a better solution to whatever the real problem is. Perhaps you can tell us more about what you’re trying to do. Where do the arrays come from? Are they produced by other Julia code?

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [October 1, 2018, 11:31pm UTC](https://discourse.julialang.org/t/parsing-a-listing-of-non-rectangular-array/15762/4 "2018-10-01T23:31:26Z")

</div>

@rdeits The arrays are read from text file. They are 3 dimensional, Nx2xM shape, non-rectangular (the last dimension has different lengths).

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 2, 2018, 12:08am UTC](https://discourse.julialang.org/t/parsing-a-listing-of-non-rectangular-array/15762/5 "2018-10-02T00:08:04Z")

</div>

I guess my question was more along the lines of: do you control the production of these text files too? If so, it might make your life easier to use a well-defined format like JSON, BSON, or JLD2 to store the data so that you can avoid `eval` and `parse`.

Actually, your data _looks_ like it could be parsed as JSON:

```julia
(v1.0) pkg> add JSON
 Resolving package versions...
  Updating `~/.julia/environments/v1.0/Project.toml`
 [no changes]
  Updating `~/.julia/environments/v1.0/Manifest.toml`
 [no changes]

julia> using JSON

julia> JSON.parse("[[[1], [2]], [[2, 3], [4, 5]], [[6, 7, 8], [3, 1, 4]]]")
3-element Array{Any,1}:
 Any[Any[1], Any[2]]            
 Any[Any[2, 3], Any[4, 5]]      
 Any[Any[6, 7, 8], Any[3, 1, 4]]

```

which is (a) about 100 times faster and (b) doesn’t create the substantial security and stability risk of `eval()`:

```julia
julia> using BenchmarkTools

julia> s = "[[[1], [2]], [[2, 3], [4, 5]], [[6, 7, 8], [3, 1, 4]]]"
"[[[1], [2]], [[2, 3], [4, 5]], [[6, 7, 8], [3, 1, 4]]]"

julia> @btime JSON.parse($s)
  4.532 μs (45 allocations: 2.59 KiB)
3-element Array{Any,1}:
 Any[Any[1], Any[2]]            
 Any[Any[2, 3], Any[4, 5]]      
 Any[Any[6, 7, 8], Any[3, 1, 4]]

julia> @btime eval(Meta.parse($s))
  318.905 μs (106 allocations: 7.22 KiB)
3-element Array{Array{Array{Int64,1},1},1}:
 [[1], [2]]            
 [[2, 3], [4, 5]]      
 [[6, 7, 8], [3, 1, 4]]

```

One difference is that `JSON.parse` (intentionally) produces `Array{Any}` containers, but it’s pretty straightforward to narrow those array types if you want:

```julia
julia> narrow(x::AbstractArray) = collect(narrow.(x))
narrow (generic function with 1 method)

julia> narrow(x) = x
narrow (generic function with 2 methods)

julia> narrow(JSON.parse(s))
3-element Array{Array{Array{Int64,1},1},1}:
 [[1], [2]]            
 [[2, 3], [4, 5]]      
 [[6, 7, 8], [3, 1, 4]]

```

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [October 2, 2018, 3:11am UTC](https://discourse.julialang.org/t/parsing-a-listing-of-non-rectangular-array/15762/6 "2018-10-02T03:11:03Z")

</div>

Thank you. `JSON.parse` solves my problem. The data comes from 3rd party, so I’m stuck with their format.
