# I made a recursive function to extract a value from a JSON given the key for that

**URL:** https://discourse.julialang.org/t/i-made-a-recursive-function-to-extract-a-value-from-a-json-given-the-key-for-that/56278
**Category:** General Usage
**Created:** [March 1, 2021, 7:16pm UTC](https://discourse.julialang.org/t/i-made-a-recursive-function-to-extract-a-value-from-a-json-given-the-key-for-that/56278 "2021-03-01T19:16:50Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)
#### Post date: [March 1, 2021, 7:16pm UTC](https://discourse.julialang.org/t/i-made-a-recursive-function-to-extract-a-value-from-a-json-given-the-key-for-that/56278/1 "2021-03-01T19:16:51Z")

</div>

I made a recursive function to extract a value from a JSON given the key for that value:

```julia
function extract(json, key)
    req = []

    for (k, v) in pairs(json)
        if k == key
            println("FOUND $key == $k")
            println("value: $v")
            println("type: $(typeof(v))")
            push!(req, v)
        elseif typeof(v) &lt;: AbstractDict
            extract(v, key)
        end
    end
    req
end

```

I tested this function with the following `Dict`: `Dict(:a =&gt; Dict(:b =&gt; "wrong"), :Src =&gt; "GOLD")`  
With the expected result: It returns an `Array{Any, 1}` with the value `"GOLD"`

However, when I try it with the real use case in mind it doesn’t return anything. The real use case is that I get two JSON payloads from an HTTP request and read them using `JSON3.read()` . I then put those two JSONs in a dict. The resulting type of the Dict is: `Dict{Symbol,JSON3.Object{Array{UInt8,1},Array{UInt64,1}}}` .  
The print statements run when it finds the key but for some reason it doesn’t push the value (`v`) to the `req` List and so it returns nothing. The same thing happens if I substitute the `push!()` with a `return v` instead. Nothing is returned.

Any ideas?

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1614625897144400?thread_ts=1614625897.144400&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

### Author: ![franco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/franco/32/16905_2.png) [@franco](https://discourse.julialang.org/u/franco)
#### Post date: [March 1, 2021, 9:16pm UTC](https://discourse.julialang.org/t/i-made-a-recursive-function-to-extract-a-value-from-a-json-given-the-key-for-that/56278/2 "2021-03-01T21:16:47Z")

</div>

Some of the symbols got mangled when posting from Slack.  
The function was tested with a small `Dict` that can be reproduced like so:  
`test_dict = Dict(:a => Dict(:Src => "GOLD"), :Channel => "x")`  
And the real case `Dict` looks something like this:

```julia
Dict(
    :app => {
        :TrafficSource => "x"
        :Label => {
            :Channel => "y"
            :Domain => ".com"
            :Src => "123"
        }
    }
)

```

I have come up with a working solution now:

```julia
function extract_json(json, key)
    req = []
    function extract(json, key)
        for (k, v) in pairs(json)
            if k == key
                push!(req, v)
            elseif typeof(v) <: AbstractDict
                extract(v, key)
            end
        end
    end
    extract(json, key)
    Set(req)
end

```

The weird thing is that a version were I don’t push to an Array an instead just return the value when the key is found doesn’t return the value even if the first conditional branch is entered. (Which I checked with some print statements). I’m looking for something like this:

```julia
function extract2(json, key)
    for (k, v) in pairs(json)
        if k == key
            return v
        elseif typeof(v) <: AbstractDict
            extract(v, k)
      end
    end
end

```
