# JSON3 indexing nested object by string path

**URL:** https://discourse.julialang.org/t/json3-indexing-nested-object-by-string-path/109056
**Category:** General Usage
**Tags:** json3
**Created:** [January 20, 2024, 9:33pm UTC](https://discourse.julialang.org/t/json3-indexing-nested-object-by-string-path/109056 "2024-01-20T21:33:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![alexgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexgr/32/34616_2.png) [@alexgr](https://discourse.julialang.org/u/alexgr)
#### Post date: [January 20, 2024, 9:33pm UTC](https://discourse.julialang.org/t/json3-indexing-nested-object-by-string-path/109056/1 "2024-01-20T21:33:19Z")

</div>

Given:

```julia
using JSON3

json_string = """
{
    "name": "rocket1",
    "observations": [
        {
            "speed": "100",
            "acceleration": "1"
        },
        {
            "speed": "200",
            "acceleration": "2"
        }
    ]
}
"""

json = JSON3.read(json_string)

```

json leaves then can be indexed with the following paths:

[:name]  
[:observations][1][:speed]  
[:observations][1][:acceleration]  
[:observations][2][:speed]  
[:observations][2][:acceleration]

for example:

```julia

json[:observations][2][:speed] # returns "200"

```

How could it be indexed if the path was stored in a string? Like this:

```julia
path = "[:observations][2][:speed]"
json[path] # how? any clue?

```

Thank you

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 20, 2024, 9:54pm UTC](https://discourse.julialang.org/t/json3-indexing-nested-object-by-string-path/109056/2 "2024-01-20T21:54:49Z")

</div>

> [@alexgr](#):
>
> How could it be indexed if the path was stored in a string? Like this:
> 
> `path = "[:observations][2][:speed]"`

You’d have to do some parsing of the string. Wouldn’t it be easier to store a tuple of indices, e.g. `(:observations, 2, :speed)`?

---

<div class="post-metadata">

### Author: ![alexgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexgr/32/34616_2.png) [@alexgr](https://discourse.julialang.org/u/alexgr)
#### Post date: [January 20, 2024, 10:07pm UTC](https://discourse.julialang.org/t/json3-indexing-nested-object-by-string-path/109056/3 "2024-01-20T22:07:14Z")

</div>

> [@stevengj](#):
>
> You’d have to do some parsing of the string. Wouldn’t it be easier to store a tuple of indices, e.g. `(:observations, 2, :speed)`?

Nice! Yes, it is.

```julia

function get(json, path)
    result = json
    for node in path
        result = getindex(result, node)
    end
    result
end

path = (:observations, 2, :speed)
get(json, path)

```
